如何在android中解析这种类型的json数组

时间:2017-07-12 11:49:01

标签: android

这是我有的数组

[ {
        "comment": "Sir, 2nd paper qualifying hai ya Uski bhi merit banegi?",
        "user": "Sameer",
        "image": "https://jdcivils.org/images/co.png",
        "date": "04/Sep/2016",
        "reply": [
            {
                "comment": "Abhi qualifying nhi hai,iske marks  merit list me judte hain",
                "user": "Admin",
                "image": "https://jdcivils.org/images/co.png",
                "date": "04/Sep/2016"
            }
        ]
    },
    {
        "comment": "Cgpsc me hight kis kis post ke liye jaruri hoti hai..\r\nplss btayega koi??",
        "user": "Vinod kumar Yadav",
        "image": "https://jdcivils.org/images/co.png",
        "date": "29/Aug/2016",
        "reply": ""
    },
    {
        "comment": "Sir Cgpsc Me gen. category walo Ko Psc k attempts ki koi limit Hoti Hai kya??",
        "user": "Vinod kumar Yadav",
        "image": "https://jdcivils.org/images/co.png",
        "date": "28/Aug/2016",
        "reply": [
            {
                "comment": "nahi",
                "user": "Admin",
                "image": "https://jdcivils.org/images/co.png",
                "date": "28/Aug/2016"
           }
        ]
    }]

6 个答案:

答案 0 :(得分:1)

尝试这种方式

JSONArray jsonarray = new JSONArray(yourresponse))
for(int i=0;i<jsonarray.length;i++){
JSONObject jsonobject = jsonarray.getJsonObject(i);
String comment = jsonobject.getString("comment");
String user = jsonobject.getString("user");
String image = jsonobject.getString("image");
String date = jsonobject.getString("date");


JSONArray jsonarray1 = jsonobject.getJSONArray("reply");
for(int j=0; j<jsonarray1.length;j++){
String comment1 = jsonobject.getString("comment");
String user1 = jsonobject.getString("user");
String image1 = jsonobject.getString("image");
String date1 = jsonobject.getString("date");


}




}

答案 1 :(得分:1)

我假设您在respose中获取字符串,因此将其转换为jsonArray

JSONArray SamplejsonArray = new JSONArray(String);
      for(int i = 0;i<SamplejsonArray .length();i++){
         JSONObject SamplejsonObject = SamplejsonArray .getJsonObject(i);
         String comment = SamplejsonObject .getString("comment");
         String user = SamplejsonObject .getString("user");
         String image= SamplejsonObject .getString("image");
         String date= SamplejsonObject .getString("date");
JSONArray replyjsonArray = SamplejsonObject.getJSONArray(reply);

for(int j = 0;i<replyjsonArray .length();j++){
JSONObject replyjsonObject = replyjsonArray.getJsonObject(j);
        String comment = replyjsonObject .getString("comment");
         String user = replyjsonObject .getString("user");
         String image= replyjsonObject .getString("image");
         String date= replyjsonObject .getString("date");
}

    }

答案 2 :(得分:0)

使用json to pojo converter从复杂的json数组创建模型类

参考链接:http://www.jsonschema2pojo.org/

答案 3 :(得分:0)

使用此库,您只需创建与您拥有的字符串相同的模型。 Gson parser

<Your model> value = new Gson().fromJson(new String(<Ur string>.getBytes("ISO-8859-1"),
                "UTF-8"), <Your model>.class);

答案 4 :(得分:0)

试试这种方式

      JSONArray jsonArray = new JSONArray(sampleString);
      for(int i = 0;i<jsonArray.length();i++){
         JSONObject jsonObject = jsonArray.getJsonObject(i)
         String comment = jsonObject.getString("comment");
         String user = jsonObject.getString("user");
         JSONArray replyArray = jsonObject.getJSONArray("reply");
         for(int j = 0;j<replyArray.length();j++){
              JSONObject innerObject = replyArray.getJSONObject(j);
              String comment = innerObject.getString("comment"); ...
         }
    }

答案 5 :(得分:0)

try {
    //Create array object from String object that contains your json array
    JSONArray jArray = new JSONArray(jsonString);

    //Get JSONObject from array
    jArray.get(0);
} catch (JSONException e) {
    e.printStackTrace();
}