2017-04-01 117 views
0

我試圖從以下JSON數組中獲取項目名稱,品牌名稱和總碳水化合物值,但在訪問「字段」部分中的各個值時遇到問題。任何人有任何指針來檢索這個信息?如何解析兩個JSON數組中的JSON對象?

{ 

「total_hits」:49127, 「MAX_SCORE」:11.919899, 「點擊」:[{

"_index": "f762ef22-e660-434f-9071-a10ea6691c27", 
    "_type": "item", 
    "_id": "513fceb375b8dbbc21000022", 
    "_score": 11.919899, 
    "fields": { 
    "item_id": "513fceb375b8dbbc21000022", 
    "item_name": "Cheese, cheddar - 1 cup, diced", 
    "brand_name": "USDA", 
    "nf_total_carbohydrate": 4.08, 
    "nf_serving_size_qty": 1, 
    "nf_serving_size_unit": "serving" 
    } 
}, 
{ 
    "_index": "f762ef22-e660-434f-9071-a10ea6691c27", 
    "_type": "item", 
    "_id": "513fceb375b8dbbc21000021", 
    "_score": 11.788424, 
    "fields": { 
    "item_id": "513fceb375b8dbbc21000021", 
    "item_name": "Cheese, cheddar - 1 cup, melted", 
    "brand_name": "USDA", 
    "nf_total_carbohydrate": 7.54, 
    "nf_serving_size_qty": 1, 
    "nf_serving_size_unit": "serving" 
    } 

/*對不起由於某種原因,我不能得到正確的格式,但在「命中」是整個的父突出顯示的代碼段*/

+0

發佈正確格式JSON響應... – rafsanahmad007

回答

1

試試這個:

try { 
     JSONObject object = new JSONObject(json); 
     JSONArray hits = object.getJSONArray("hits"); 
     for (int i = 0; i < hits.length(); i++) { 
      JSONObject fields = hits.getJSONObject(i).getJSONObject("fields"); 
      String itemName = fields.getString("item_name"); 
      String brandName = fields.getString("brand_name"); 
      double carbohydrate = fields.getDouble("nf_total_carbohydrate"); 
      Log.d("HitTag", itemName+" "+brandName+" "+carbohydrate); 
     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

我假設你有這樣的JSON:

{ 
    "total_hits": 49127, 
    "max_score": 11.919899, 
    "hits": [ 
    { 
     "_index": "f762ef22-e660-434f-9071-a10ea6691c27", 
     "_type": "item", 
     "_id": "513fceb375b8dbbc21000022", 
     "_score": 11.919899, 
     "fields": { 
     "item_id": "513fceb375b8dbbc21000022", 
     "item_name": "Cheese, cheddar - 1 cup, diced", 
     "brand_name": "USDA", 
     "nf_total_carbohydrate": 4.08, 
     "nf_serving_size_qty": 1, 
     "nf_serving_size_unit": "serving" 
     } 
    }, 
    { 
     "_index": "f762ef22-e660-434f-9071-a10ea6691c27", 
     "_type": "item", 
     "_id": "513fceb375b8dbbc21000021", 
     "_score": 11.788424, 
     "fields": { 
     "item_id": "513fceb375b8dbbc21000021", 
     "item_name": "Cheese, cheddar - 1 cup, melted", 
     "brand_name": "USDA", 
     "nf_total_carbohydrate": 7.54, 
     "nf_serving_size_qty": 1, 
     "nf_serving_size_unit": "serving" 
     } 
    } 
    ] 
} 
+0

這真是棒極了三江源! –

0

這裏是全工作代碼。試試這個:

// Your JSON string 
String jsonStr = "{\n" + 
      " \"total_hits\": 49127,\n" + 
      " \"max_score\": 11.919899,\n" + 
      " \"hits\": [\n" + 
      " {\n" + 
      "  \"_index\": \"f762ef22-e660-434f-9071-a10ea6691c27\",\n" + 
      "  \"_type\": \"item\",\n" + 
      "  \"_id\": \"513fceb375b8dbbc21000022\",\n" + 
      "  \"_score\": 11.919899,\n" + 
      "  \"fields\": {\n" + 
      "  \"item_id\": \"513fceb375b8dbbc21000022\",\n" + 
      "  \"item_name\": \"Cheese, cheddar - 1 cup, diced\",\n" + 
      "  \"brand_name\": \"USDA\",\n" + 
      "  \"nf_total_carbohydrate\": 4.08,\n" + 
      "  \"nf_serving_size_qty\": 1,\n" + 
      "  \"nf_serving_size_unit\": \"serving\"\n" + 
      "  }\n" + 
      " },\n" + 
      " {\n" + 
      "  \"_index\": \"f762ef22-e660-434f-9071-a10ea6691c27\",\n" + 
      "  \"_type\": \"item\",\n" + 
      "  \"_id\": \"513fceb375b8dbbc21000021\",\n" + 
      "  \"_score\": 11.788424,\n" + 
      "  \"fields\": {\n" + 
      "  \"item_id\": \"513fceb375b8dbbc21000021\",\n" + 
      "  \"item_name\": \"Cheese, cheddar - 1 cup, melted\",\n" + 
      "  \"brand_name\": \"USDA\",\n" + 
      "  \"nf_total_carbohydrate\": 7.54,\n" + 
      "  \"nf_serving_size_qty\": 1,\n" + 
      "  \"nf_serving_size_unit\": \"serving\"\n" + 
      "  }\n" + 
      " }\n" + 
      " ]\n" + 
      "}"; 

    try { 
     JSONObject jsonObject = new JSONObject(jsonStr); 
     JSONArray jsonArrayHits = jsonObject.getJSONArray("hits"); 

     // Get all jsonObject from jsonArray 
     for (int i = 0; i < jsonArrayHits.length(); i++) 
     { 
      JSONObject jsonObjectFields = jsonArrayHits.getJSONObject(i).getJSONObject("fields"); 

      String itemName = null, brandName = null; 
      double totalCarbohydrate = 0.0; 

      // Item name 
      if (jsonObjectFields.has("item_name") && !jsonObjectFields.isNull("item_name")) { 
       itemName = jsonObjectFields.getString("item_name"); 
      } 

      // Brand name 
      if (jsonObjectFields.has("brand_name") && !jsonObjectFields.isNull("brand_name")) { 
       brandName = jsonObjectFields.getString("brand_name"); 
      } 

      // Total carbohydrate 
      if (jsonObjectFields.has("nf_total_carbohydrate") && !jsonObjectFields.isNull("nf_total_carbohydrate")) { 
       totalCarbohydrate = jsonObjectFields.getDouble("nf_total_carbohydrate"); 
      } 

      Log.d("SUCCESS", "JSON Object: " + "\nItem Name: " + itemName 
        + "\nBrand Name: " + brandName 
        + "\nTotal carbohydrate: " + totalCarbohydrate); 
     } 
    } catch (JSONException e) { 
     Log.e("FAILED", "Json parsing error: " + e.getMessage()); 
    } 

輸出日誌:

D/SUCCESS: JSON Object: 
      Item Name: Cheese, cheddar - 1 cup, diced 
      Brand Name: USDA 
      Total carbohydrate: 4.08 

D/SUCCESS: JSON Object: 
      Item Name: Cheese, cheddar - 1 cup, melted 
      Brand Name: USDA 
      Total carbohydrate: 7.54 

希望這將有助於〜