2

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

    }
Vadzim
  • 24,954
  • 11
  • 143
  • 151
ULLAS K
  • 881
  • 2
  • 11
  • 24

5 Answers5

3

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);
    }
}
AnnieFromTaiwan
  • 3,845
  • 3
  • 22
  • 38
user1983527
  • 304
  • 1
  • 7
1

Use this: Assuming your JSONArray is net.sf.json.JSONArray

String str = "[ {name:\"214\",value:true,Id:0},{name:\"215\",value:true,Id:0},{name:\"216\",value:true,Id:0}]";
JSONArray array = JSONArray.fromObject(str);
Srinivas
  • 1,780
  • 1
  • 14
  • 27
1

I just combined some of the answer and found the right solution here is code ..

String str = "[ {name:\"214\",value:true,Id:1},{name:\"215\",value:false,Id:2},{name:\"216\",value:true,Id:3}]";
    JSONArray array = JSONArray.fromObject(str);
    for (Object object : array) {
        JSONObject jsonStr = (JSONObject)JSONSerializer.toJSON(object);
          System.out.println(" name is --"+jsonStr.get("name"));
            System.out.println(" value is ---"+jsonStr.get("value"));
            System.out.println(" id is ----"+jsonStr.get("Id"));
    }
ULLAS K
  • 881
  • 2
  • 11
  • 24
0

You can do as below,

String str = "[ {name:\"214\",value:true,Id:0},{name:\"215\",value:true,Id:0},{name:\"216\",value:true,Id:0}]";
JsonParser parser = new JsonParser();
JsonElement element = parser.parse(str);
JsonArray jasonArray = element.getAsJsonArray();

If you are using import com.google.gson.*;

Jayamohan
  • 12,734
  • 2
  • 27
  • 41
0

In case you have DTO class, the mapping can be done automatically via reflection:

import net.sf.json.JSONArray;

List<DTO> list = (List<DTO>) JSONArray.toList(JSONArray.fromString(str), DTO.class);
Vadzim
  • 24,954
  • 11
  • 143
  • 151