1

I am working on an android project. I need help in this JSON data parsing which I am retrieving from the server:

{"item_name":["Navnita","Navnita","Navnita"],"username":["James","John","Jenny"],"review":["Its a relly nice restaurant for Vegetarians! :)","Cool!","Food is just great!"]}

I just want to populate the RecyclerView with the "username" and his "review". Right now I am using the following code to parse the JSON data and populate an arrayList which in turn populates the RecyclerView but it gives me error(logcat below):

String result ....;
           JSONObject jObject = new JSONObject(result);
            JSONArray usernameJsonArray =  jObject.getJSONArray("username");
            JSONArray reviewJsonArray = jObject.getJSONArray("review");
            for(int i=0;i<usernameJsonArray.length();i++){
                String username = usernameJsonArray.getJSONObject(i).toString();
                String review = reviewJsonArray.getJSONObject(i).toString();
                getReviewFeedItemsArrayList.add(new GetReviewFeedItems(username,review));
            }

I think that the problem is with this above code but I am not able to figure it out. Here's the logcat:

09-19 14:33:24.295  30572-30646/app.usrete.jayant.delvemitt W/System.err﹕ org.json.JSONException: Value James at 0 of type java.lang.String cannot be converted to JSONObject
09-19 14:33:24.305  30572-30646/app.usrete.jayant.delvemitt W/System.err﹕ at org.json.JSON.typeMismatch(JSON.java:100)
09-19 14:33:24.305  30572-30646/app.usrete.jayant.delvemitt W/System.err﹕ at org.json.JSONArray.getJSONObject(JSONArray.java:484)
1
  • Do you control the data coming from the server? It might be beneficial to restructure your JSON something like {["item":"Nanvita", "username":"James", "review": "Best ever!"], [...], [...]}. For your question: looks like @Ravi got it! Commented Sep 19, 2015 at 9:33

4 Answers 4

3

directly write this in your for loop

String username=usernameJsonArray.getString(i);

Your inner data is not JsonObject its just string

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

Comments

1

you use the following to code to retrieve the data

            JSONObject jObject = new JSONObject(result);
            JSONArray usernameJsonArray =  jObject.getJSONArray("username");
            JSONArray reviewJsonArray = jObject.getJSONArray("review");
            for(int i=0;i<usernameJsonArray.length();i++)
             {
                String username = usernameJsonArray.getString(i);
                String review = reviewJsonArray.getString(i);
                getReviewFeedItemsArrayList.add(new GetReviewFeedItems(username,review));
            }

Comments

0

I think you are getting extra html junks with your echoed JSON format output from the http response.

Just add this after you convert your http response to string. This code will trim off the extra html junks and spit out the json format.

jsonData = jsonData.substring(jsonData.indexOf("{\""), jsonData.lastIndexOf("\"}") + 2);

Here "jsonData" is the String format from your http response. After this follow the usual steps for JSON Parsing.

Comments

0

I'd use GSON. First convert your JSON into POJO classes, http://www.jsonschema2pojo.org/. Then parse your JSON data with GSON:

Gson gson = new Gson();
PojoClass obj = gson.fromJson(jsonString, PojoClas.class);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.