1

I am new to android and stuck with some problem in Json parsing.

JSONArray resultJsonArray = data.getJSONArray("detailsArr");

getJSONArray is showing in red colour it means some issue is with getJSONArray. But I am not able to resolve it.

JSON:

{"msg":"","status":true,"result":[{"conversation":"<p>sani<\/p>","attachmentName":"","attachmentURL":"","clientType":"student","repliedOn":"30-Sep-15 11:19AM","expertName":"shubham goyal","expertPic":""},{"conversation":"<p>rere<\/p>","attachmentName":"","attachmentURL":"","clientType":"expert","repliedOn":"1-Oct-15 5:31PM","expertName":"shubham goyal","expertPic":""},{"conversation":"<p>all vl<\/p>","attachmentName":"","attachmentURL":"","clientType":"student","repliedOn":"1-Oct-15 5:44PM","expertName":"shubham goyal","expertPic":""},{"conversation":"<p>asa kk<\/p>","attachmentName":"","attachmentURL":"","clientType":"expert","repliedOn":"1-Oct-15 5:45PM","expertName":"shubham goyal","expertPic":""}]}
11
  • Is data a JSONArray or Object?.. Commented Dec 21, 2015 at 13:47
  • JSONArray but tried with JSONObject also.still it is not working. Commented Dec 21, 2015 at 13:48
  • post your code . i think problem here data Commented Dec 21, 2015 at 13:49
  • 1
    @IntelliJAmiya Last time also you helped me I think today also you can help me :) Commented Dec 21, 2015 at 13:53
  • Your JSON is an Object not an Array. Post your parsing code. Commented Dec 21, 2015 at 13:54

5 Answers 5

1

According to your JSON

it should be

 JSONArray resultJsonArray = data.getJSONArray("result");

Since result is the name of the array.

Sign up to request clarification or add additional context in comments.

Comments

1

I did't find detailsArr .

JSONObject reader = new JSONObject(Your_Json_Sring);

                JSONArray jsonArray = reader.getJSONArray("result");


                for (int i = 0; i < jsonArray.length(); i++)
                {
                    JSONObject e = jsonArray.getJSONObject(i);

                    String conversation = e.getString("conversation");
                  }

Comments

0

Bundle doesn't have a method getJSONArray()

And your JSON string doesn't contain a key named detailsArr.

UPDATE:

I think what you want to do is this:

JSONArray resultJsonArray = new JSONObject(data.getString("detailsArr"))
        .getJSONArray("result");

3 Comments

Lets think result is detailsArr
how to access jsonarray from bundle
@Shivam I updated my answer. You should create a JSONObject first. From the JSON-formatted string, you can create one. Then you can call getJSONArray() with the key 'result'
0

Try following things:

  1. Check imports, it should be like (if you are using other library - should be similar)

    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
    
  2. Check if you data is type of JSONObject.

Comments

0

Step 1: Using this Link Create one Pojo for your Json String.

Step 2: put following in your gradle than sync your gradle file

compile 'com.google.code.gson:gson:2.4'

Step :3 and at Last Use following code for deserialized Json

     Gson mGson = new Gson();
                    JsonReader reader = new JsonReader(new StringReader(YOUR_JSON_STRING));
                    final YOUR_POJO_CLASS_NAME mList = mGson.fromJson(reader, new TypeToken<YOUR_POJO_CLASS_NAME>() {
                    }.getType());

I hope your are clear with my solution. Best of Luck

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.