7

Please, please help me.. I am working on a project and I am getting data from web-services in JSON format. I am trying to parse it but, I am unable to do it. I have this json-data-

 {
        "response": {
            "status": {
                "code": "1",
                "message": "sucess",
                "user_id": "1"
            },
            "foods": [
                {
                    "char": "A",
                    "content": [
                        {
                            "food_name": "add Malt"
                        },
                        {
                            "food_name": "a la mode"
                        },
                        {
                            "food_name": "Almonds"
                        }
                 ]
            },
            {
                "char": "Z",
                "content": [
                    {
                        "food_name": "Zebra Cakes"
                    },
                        {
                            "food_name": "Zucchini, Baby"
                        },
                        {
                            "food_name": "zxc"
                        }
                    ]
                }
            ]
        }
    }

From here I am successfully able to get "foods" Array but I am getting stuck when I am trying to get "content" array and food_name data.

I am using this code but I did not get any solution, please check this snip code.

protected String doInBackground(String... args) {
                // Building Parameters
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("method","eat_tracking_details"));
                nameValuePairs.add(new BasicNameValuePair("uid",userid));
                // getting JSON string from URL
                JSONObject json = jsonParser.makeHttpRequest(JSONParser.urlname,"GET", nameValuePairs);
                //System.out.println("****json*"+json);
                if (json != null) {
                    try {
                        JSONObject response = json.getJSONObject("response");
                        JSONObject status = response.getJSONObject("status");
                        code = status.getString("code");
                        JSONArray FoodArray = response.getJSONArray("foods");
                        for (int i = 0; i < FoodArray.length(); i++) {
                            String character = FoodArray.getJSONObject(i).getString("char");
                            System.out.println("*****character****************"+character);
                            JSONArray FoodNameArray = new JSONArray(FoodArray.getJSONObject(i).getString("content"));
                            System.out.println("====================///////////"+FoodNameArray);

                            for (int j = 0; j <FoodNameArray.length(); j++) {
                                String Foodname = FoodArray.getJSONObject(j).getString("food_name");
                                System.out.println("@@@@@@@@@@@@@"+Foodname);
                            }
                        }

                    } catch (JSONException e) {
                        // TODO: handle exception
                    }
                }

Check this url for web-service response- WEB-SERVICE URL

9
  • You don't have to instantiate a new JSONArray, simply get it as you did for the foods array with the getJSONArray(). Commented Oct 15, 2013 at 7:26
  • array is not in correct format and see this androidhive.info/2012/01/android-json-parsing-tutorial Commented Oct 15, 2013 at 7:27
  • @fasteque you mean i should do like_JSONArray FoodArray = response.getJSONArray("content");? Commented Oct 15, 2013 at 7:28
  • @Rohit have you check given below link? Please use jsonlint array is ok no issue in array. Commented Oct 15, 2013 at 7:29
  • @DharaShah in case of gson I don't have to do manual parsing? do you have any demo please? Commented Oct 15, 2013 at 7:30

2 Answers 2

4

You need to replace your respective part of code with this code:

for (int j = 0; j < FoodNameArray.length(); j++) {
    String Foodname = FoodNameArray.getJSONObject(j).getString("food_name");
    System.out.println("@@@@@@@@@@@@@" + Foodname);
}
Sign up to request clarification or add additional context in comments.

3 Comments

yes dear you are right, its working... very silly mistake :) Thanks!
Then plz accept it as the correct answer so that other users can also benefit from it
Yes sure! and please if it is possible give one up-vote to my question .
2

I believe the best approach would use of GSON library (http://code.google.com/p/google-gson/) . In that case you just have to make your model classes and don't worry about the parsing logic.

1 Comment

Dear can you check please that URL? and parse with out gson library. Everything okay just i am doing any silly mistake only.

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.