-2

this is array which i have

[ {
        "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"
           }
        ]
    }]
Chirag
  • 56,621
  • 29
  • 151
  • 198
Paramatma Sharan
  • 437
  • 5
  • 11

6 Answers6

1

Try in this way

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


}




}
Sunil P
  • 3,698
  • 3
  • 13
  • 20
1

I am assuming you are getting string in respose so convert it to 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");
}

    }
Avinash
  • 264
  • 5
  • 15
0

Use json to pojo converter for create model class from complex json array

Refer link : http://www.jsonschema2pojo.org/

EKN
  • 1,886
  • 1
  • 16
  • 29
0

Use this library, all you need is create model with same key as string that you have. Gson parser

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

try this way

      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"); ...
         }
    }
debugger
  • 580
  • 1
  • 6
  • 16
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();
}