سؤال

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

    }
هل كانت مفيدة؟

المحلول

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

نصائح أخرى

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

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

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.*;

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);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top