Cris
Cris

Reputation: 437

Converting array list object to jsonarray

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.

Upvotes: 0

Views: 2371

Answers (4)

kyleruss
kyleruss

Reputation: 497

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}]
}

Upvotes: 2

Prexx
Prexx

Reputation: 2979

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);

Upvotes: 2

Diksha Dhiman
Diksha Dhiman

Reputation: 73

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.

Upvotes: 1

Kunu
Kunu

Reputation: 5134

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();
}

Upvotes: 1

Related Questions