0

I have arraylist that needs to be converted to json array to post in the server

the format of json should be like this:

{
   p:1,
   s:["a":1,"b":2],["a":2,"b":3],["a":3,"b":4]
}

Let say I have:

List<MyObject> objectList = new ArrayList<MyObject>();
MyObject myObject = new MyObject();
myObject .setA(1);
myObject .setB(2);

myObject .setA(2);
myObject .setB(3);

myObject .setA(3);
myObject .setB(4);

objectList.add(myObject);

My current code is:

   for (int i = 0; i < objectList.size(); i++) {
        JSONArray ar = new JSONArray();
        ar.put("a:"+objectList.get(i).getA());
        ar.put("b:"+objectList.get(i).getB());

        jOuter.put("s",ar);
    }

but this doesnt work, the current return is:

{"p":1,"s":["a:20","b:10.0"]}

Thanks.

8
  • That json you show is far from valid. Keys must be strings, values can be lists [ ], but lists cannot contain key value pairs, those go in objects { } Commented Sep 29, 2015 at 8:47
  • @Cris You can look into Gson library.. Commented Sep 29, 2015 at 8:53
  • you are putting to your JSONArray a String, while you should put JSONObject Commented Sep 29, 2015 at 8:58
  • how about putting it in a hashmap then add it to json? i just need the json format above. Commented Sep 29, 2015 at 9:01
  • do you think {"p":1, "s"[{"a":1,"b":2},{"a":2,"b":3}] are valid and the same that i need? Commented Sep 29, 2015 at 9:03

4 Answers 4

2

Shouldn't it be something like this?

JSONArray ar = new JSONArray();
for (int i = 0; i < objectList.size(); i++) {

        JSONObject objects = new JSONObject();
        objects.put("a", objectlist.get(i).getA();
        objects.put("b", objectlist.get(i).getB();

        ar.put(objects);

    }
        jOuter.put("s",ar);
Sign up to request clarification or add additional context in comments.

2 Comments

return is {"p":1,"s":[{"a":1,"b":2},{}]}
Someone already did it: the JSON array has to be outside the for loop
2

Your s entry is actually a JsonArray, while your p is just a property. Here I am using Gson but you could easily apply it to whichever library you are using. So you can do the following:

JsonObject jObj = new JsonObject();
jObj.addProperty("p", 1);

JsonArray sArr  = new JsonArray();

 for (int i = 0; i < objectList.size(); i++)
 {
        JsonObject innerObj = new JsonObject();
        innerObj.addProperty("a:"+objectList.get(i).getA());
        innerObj.addProperty("b:"+objectList.get(i).getB());

        sArr.add(innerObj);
 }

 jObj.addProperty("s", sArr);

The output is:

{
  "p":1,
  "s":[{"a":1,"b":2},{"a":3,"b":4}]
}

Comments

1

Your required format is invalid. I think you are looking for something like this

{
  "p": 1,
  "s": [
    {
      "a": 1,
      "b": 2
    },
    {
      "a": 2,
      "b": 3
    },
    {
      "a": 3,
      "b": 4
    }
  ]
}

For this code will be like as below

JSONArray jArray = new JSONArray();
for (int i = 0; i < objectList.size(); i++) {
        JSONObject job= new JSONObject();
        job.put("a",objectList.get(i).getA());
        job.put("b",objectList.get(i).getB());

        jArray .put(job);
}

try {
        jOuter.put("s", jArray );
} catch (JSONException e) {
        e.printStackTrace();
}

Comments

1

Please check following code: This will make the proper formatting:

JSONObject jo = new JSONObject();
    try {
        jo.put("p", "1");

        JSONArray jarr = new JSONArray();
        for (MyObject listItem : objectList) {
            JSONObject inner = new JSONObject();
            inner.put("a", listItem.getA());
            inner.put("b", listItem.getB());

            jarr.put(inner);
        }
        jo.put("s", jarr);
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

It will return output like:

{
  "p":1,
  "s":[{"a":1,"b":2},{"a":2,"b":3},{"a":3,"b":4}]
}

This makes a proper json, which I think you need! Do let me know, if I am wrong somewhere.

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.