0

I have a list which looks like this:

{
  "data1": "abcd",
  "data2": "efgh",
}

I'm trying to create a JSON response in this format by looping the above array and building the below:

{
  "id": "123456",
  "nestedArray": [
    {
      "data1": "abcd",
      "somedata": 1234
    },
    {
      "data2": "efgh",
      "somedata": 1234
    }
  ]
}

I've created a model to map the structure of the nested array:

public class nestedArray{
    public String data1;
    public Integer data2;

    public nestedArray(string data) {
        this.data1 = uri;
        this.data2 = 1234;
    }
}

But i'm stuck on how to build the final response, any pointers is much appreciated!

1 Answer 1

1

Try this :

JSON:

{
  "id": "123456",
  "nestedArray": [
    {
      "data1": "abcd",
      "somedata": 1234
    },
    {
      "data1": "efgh",
      "somedata": 1234
    }
  ]
}

Model:
public class Resp {

    public class NestedArray {
        public String data1;
        public Integer somedata;
    }

    public String id;
    public List<NestedArray> nestedArray;


    public static Resp parse(String json) {
        return (Resp) System.JSON.deserialize(json, Resp.class);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.