这是我从网址获取的json
{
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"
}
]
}
]
}
我试过的代码:
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);
}
但是我得到了:没有找到早餐阵列的价值。 在数据数组之后有一个{,所以我需要在调用JsonArray之前调用JsonObject吗? 任何人都可以建议我做什么
由于
答案 0 :(得分:1)
这是一种不正确的JSON格式。你必须用#34;标志。首先,试试这个。
在此测试:http://jsonlint.com/
例如,这是正确的
{
"responce": "success",
"data": [{
"Breakfast": [{
"food_interval": "Breakfast"
}]
}]
}
修改强>
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);
}
}
}
}
我测试了这个JSON。 但如果我使用JSOUP,我会收到403错误:
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();
您必须检查是否在代码中检索此JSON(而不是在浏览器中)。
答案 1 :(得分:0)
检查http://jsonlint.com/的json有效性 它现在不是一个有效的回应。
之后,创建相应的Response.java类并使用Gson解析它。只需根据您的响应编写Response.java实体。之后:
Gson gson = new GsonBuilder().create();
Response r = gson.fromJson(jsonString, Response.class);
有关更多示例,您可以按照本教程操作: http://rowsandcolumns.blogspot.com/2013/02/url-encode-http-get-solr-request-and.html
答案 2 :(得分:0)
你的json file.html没有任何html标签只是你的json