如何解析两个JSON数组中的JSON对象?

时间:2017-04-01 18:56:36

标签: android json parsing

我正在尝试从以下JSON数组中获取项目名称,品牌名称和总碳水化合物值,但是在访问“字段”部分中的各个值时遇到问题。是否有任何指针可以检索此信息?

as[0]

“total_hits”:49127,   “max_score”:11.919899,   “点击”:[     {

{

/ *抱歉由于某些原因我无法获得正确的格式,但“点击”是整个突出显示的代码部分的父级* /

2 个答案:

答案 0 :(得分: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"
      }
    }
  ]
}

答案 1 :(得分: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

希望这会有所帮助〜