0

This is the json which i am getting from a URL

{
responce: "success",
data: [
        {
        Breakfast: [
            {
                food_interval: "Breakfast",
                id: "1",
                menu_creator_id: "1",
                item_code: "13",
                food_interval_category_id: "1",
                food_interval_category: "Main Item",
                item_name: "Methi thepal",
                item_quantity: "1",
                unit_id: "1",
                unit_desc: "Number"
            },
            {
                food_interval: "Breakfast",
                id: "2",
                menu_creator_id: "1",
                item_code: "14",
                food_interval_category_id: "2",
                food_interval_category: "Cereals",
                item_name: "Museli",
                item_quantity: "1",
                unit_id: "7",
                unit_desc: "Bowl "
            },
            {
                food_interval: "Breakfast",
                id: "3",
                menu_creator_id: "1",
                item_code: "15",
                food_interval_category_id: "8",
                food_interval_category: "Bread",
                item_name: "Brown Bread",
                item_quantity: "2",
                unit_id: "1",
                unit_desc: "Number"
            },
            {
                food_interval: "Breakfast",
                id: "4",
                menu_creator_id: "1",
                item_code: "16",
                food_interval_category_id: "21",
                food_interval_category: "Butter Cheese",
                item_name: "Cheddar Cheese",
                item_quantity: "1",
                unit_id: "3",
                unit_desc: "Gram"
             }
           ]
        }
    ]
}

Code i have tried :

String result = response.body().string();
JSONObject jsonObject = new JSONObject(result);
JSONArray foodintervalarray = jsonObject.getJSONArray("data");
for(int i = 0 ; i < foodintervalarray.length(); i++){
    JSONObject jsonObject1 = foodintervalarray.getJSONObject(i);
    JSONArray breakfast = jsonObject1.getJSONArray("Breakfast");
    for(int j = 0 ; j < breakfast.length(); j++){
        JSONObject jsonObject2 = breakfast.getJSONObject(j);
        String breakFastMenu = jsonObject2.getString("food_interval");
        Log.i("breakFastMenu","breakFastMenu "+breakFastMenu);
    }

But i am getting : No value found for Breakfast array. After the data array there is a { , so do i need to call JsonObject before calling JsonArray for breakfast ? Can anybody suggest me what to do

Thanks

3 Answers 3

1

This is an incorrect JSON format. You have to wrap key by " sign. First of all, try this.

Test here : http://jsonlint.com/

E.g., this is correct

{
    "responce": "success",
    "data": [{
        "Breakfast": [{
            "food_interval": "Breakfast"
        }]
    }]
}

EDIT

import org.json.JSONArray;
import org.json.JSONObject;


public class tst {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        final String result = "{\"responce\":\"success\",\"data\":[{\"Breakfast\":[{\"food_interval\":\"Breakfast\",\"id\":\"1\",\"menu_creator_id\":\"1\",\"item_code\":\"13\",\"food_interval_category_id\":\"1\",\"food_interval_category\":\"Main Item\",\"item_name\":\"Methi thepal\",\"item_quantity\":\"1\",\"unit_id\":\"1\",\"unit_desc\":\"Number\"},{\"food_interval\":\"Breakfast\",\"id\":\"2\",\"menu_creator_id\":\"1\",\"item_code\":\"14\",\"food_interval_category_id\":\"2\",\"food_interval_category\":\"Cereals\",\"item_name\":\"Museli\",\"item_quantity\":\"1\",\"unit_id\":\"7\",\"unit_desc\":\"Bowl \"},{\"food_interval\":\"Breakfast\",\"id\":\"3\",\"menu_creator_id\":\"1\",\"item_code\":\"15\",\"food_interval_category_id\":\"8\",\"food_interval_category\":\"Bread\",\"item_name\":\"Brown Bread\",\"item_quantity\":\"2\",\"unit_id\":\"1\",\"unit_desc\":\"Number\"},{\"food_interval\":\"Breakfast\",\"id\":\"4\",\"menu_creator_id\":\"1\",\"item_code\":\"16\",\"food_interval_category_id\":\"21\",\"food_interval_category\":\"Butter Cheese\",\"item_name\":\"Cheddar Cheese\",\"item_quantity\":\"1\",\"unit_id\":\"3\",\"unit_desc\":\"Gram\"}],\"10 A. M\":[{\"food_interval\":\"10 A. M\",\"id\":\"\",\"menu_creator_id\":\"\",\"item_code\":\"\",\"food_interval_category_id\":\"\",\"food_interval_category\":\"\",\"item_name\":\"\",\"item_quantity\":\"\",\"unit_id\":\"\",\"unit_desc\":\"\"}],\"11.30 Soup\":[{\"food_interval\":\"11.30 Soup\",\"id\":\"5\",\"menu_creator_id\":\"5\",\"item_code\":\"9\",\"food_interval_category_id\":\"13\",\"food_interval_category\":\"Soup\",\"item_name\":\"Carrot Soup\",\"item_quantity\":\"1\",\"unit_id\":\"7\",\"unit_desc\":\"Bowl \"}],\"Lunch\":[{\"food_interval\":\"Lunch\",\"id\":\"6\",\"menu_creator_id\":\"6\",\"item_code\":\"15\",\"food_interval_category_id\":\"9\",\"food_interval_category\":\"Chapati\",\"item_name\":\"Brown Bread\",\"item_quantity\":\"4\",\"unit_id\":\"1\",\"unit_desc\":\"Number\"}],\"3.30 PM\":[{\"food_interval\":\"3.30 PM\",\"id\":\"\",\"menu_creator_id\":\"\",\"item_code\":\"\",\"food_interval_category_id\":\"\",\"food_interval_category\":\"\",\"item_name\":\"\",\"item_quantity\":\"\",\"unit_id\":\"\",\"unit_desc\":\"\"}],\"7 pm Soup\":[{\"food_interval\":\"7 pm Soup\",\"id\":\"\",\"menu_creator_id\":\"\",\"item_code\":\"\",\"food_interval_category_id\":\"\",\"food_interval_category\":\"\",\"item_name\":\"\",\"item_quantity\":\"\",\"unit_id\":\"\",\"unit_desc\":\"\"}],\"Dinner\":[{\"food_interval\":\"Dinner\",\"id\":\"\",\"menu_creator_id\":\"\",\"item_code\":\"\",\"food_interval_category_id\":\"\",\"food_interval_category\":\"\",\"item_name\":\"\",\"item_quantity\":\"\",\"unit_id\":\"\",\"unit_desc\":\"\"}]}]}";

        JSONObject jsonObject = new JSONObject(result);
        JSONArray foodintervalarray = jsonObject.getJSONArray("data");
        for(int i = 0 ; i < foodintervalarray.length(); i++){
            JSONObject jsonObject1 = foodintervalarray.getJSONObject(i);
            JSONArray breakfast = jsonObject1.getJSONArray("Breakfast");
            for(int j = 0 ; j < breakfast.length(); j++){
                JSONObject jsonObject2 = breakfast.getJSONObject(j);
                String breakFastMenu = jsonObject2.getString("food_interval");
                System.out.println("breakFastMenu "+breakFastMenu);
            }
        }
    }

}

I've tested this JSON. But if I use JSOUP I obtain 403 error:

final String result = Jsoup.connect("http://dieto.vm1.in/api/menu_display_2d.php?date=2016/08/22&diet_type=6&food_category=1&class_type=1").ignoreHttpErrors(true).get().data();

You have to check whether you retrieve this JSON inside the code (not in browser).

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

2 Comments

dieto.vm1.in/api/menu_display_2d.php?date=2016/08/… when i test it in jsonlint , it shows me correctly , it also says valid but in my browser it shows differently , why so ?
@narahari_arjun , updated. check how do you retrieve your json data. Is it correct.
0

Check json validity from http://jsonlint.com/ It it not a valid response right now.

After that, create corresponding Response.java class and parse it with Gson. Just write the Response.java entity according to your response.After that:

Gson gson = new GsonBuilder().create(); 
Response r = gson.fromJson(jsonString, Response.class);

For more example you can follow this tutorial : http://rowsandcolumns.blogspot.com/2013/02/url-encode-http-get-solr-request-and.html

2 Comments

hey thanx for replying , i am giving you the original link , i just posted a part of it , actually the link i had posted in jsonlint , it shows valid. In json lint , the Breakfast and the other other sub arrays are shown like this "Breakfast" but i am not able to view like this in my browser , why so ? dieto.vm1.in/api/menu_display_2d.php?date=2016/08/…
0

your json file.html dose not have any tag of html just your json

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.