How to Convert JSON String arrray to Json Array list

ULLAS K picture ULLAS K · Jan 25, 2013 · Viewed 29.8k times · Source

I have Json String array ,which looks like this ,

{ {name:"214",value:true,Id:0},
  {name:"215",value:true,Id:0},
  {name:"216",value:true,Id:0}
}

and want to covert this string to Json array object and iterate the list to read each object's value. Then set values to corresponding dto and save it . But i didnt find any good way to convert normal JSON array string to json array object.

I am not using google json , I want it to be done in normal json itself .Please help me

and java class i want something like this

JSONObject[] jsonObjectList = String after convert();

    for (JSONObject jsonObject : jsonObjectList) {
        System.out.println(" name is --"+jsonObject.get("name"));
        System.out.println(" value is ---"+jsonObject.get("value"));
        System.out.println(" id is ----"+jsonObject.get("id"));

    }

Answer

user1983527 picture user1983527 · Jan 25, 2013

Here is the example for parsing json Object.. use JSON lib for this..

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
public class TestJson {
    public static void parseProfilesJson(String jsonStr) {
        try {
            JSONArray nameArray = (JSONArray) JSONSerializer.toJSON(jsonStr);
            System.out.println(nameArray.size());
            for(Object js : nameArray){
                JSONObject json = (JSONObject) js;
                System.out.println(json.get("date"));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        String s = "[{\"date\":\"2012-04-23\",\"activity\":\"gym\"},{\"date\":\"2012-04-24\",\"activity\":\"walking\"}]";
        parseProfilesJson(s);
    }
}