2016-08-23 103 views
0

這是我從一個URL獲取JSONJSONarray分析顯示沒有價值發現

{ 
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? 有人建議我做什麼

感謝

回答

1

這是一個不正確的JSON格式。你必須通過「符號來包裝的關鍵首先,試試這個

測試此: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(不在瀏覽器中)。

+0

http://dieto.vm1.in/api/menu_display_2d.php?date=2016/08/22&diet_type=6&food_category=1&class_type=1 當我jsonlint測試它,它讓我看到正確的,它也說有效,但在我的瀏覽器中顯示不同,爲什麼這樣? –

+0

這是有點正確的json.w8。我會查的。 – Vyacheslav

+0

@narahari_arjun,已更新。檢查你如何檢索你的json數據。這是對的嗎。 – Vyacheslav

0

http://jsonlint.com/檢查json有效性 它現在不是一個有效的回覆。

之後,創建相應的Response.java類並用Gson解析它。只是根據你的response.After寫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

+0

嗨thanx回覆,我給你的原始鏈接,我剛剛發佈的一部分,實際上我張貼在jsonlint的鏈接,它顯示有效。 在json lint中,早餐和其他其他子數組顯示爲「早餐」,但我無法在瀏覽器中像這樣查看,爲什麼? http://dieto.vm1.in/api/menu_display_2d.php?date=2016/08/22&diet_type=6&food_category=1&class_type=1 –

+0

您可以使用郵差https://chrome.google.com/webstore/detail/郵遞員/ fhbjgbiflinjbdggehcddcbncdddomop?HL = EN –

0

您的JSON file.html劑量不會有任何HTML標籤只是您的JSON