【问题标题】:Get value from JSON Response containing nested name value pairs based on a name value in Java [duplicate]从包含基于Java中名称值的嵌套名称值对的JSON响应中获取值[重复]
【发布时间】:2018-04-09 08:04:16
【问题描述】:

我需要使用Java从下面的JSON响应中的“full_nutrients”数组中的“attr_id”获取相应的“值”,如何实现?例如,我想在“attr_id”== 205 时获取值。

JSON 响应:

"foods": [
    {
        "food_name": "chicken noodle soup",
        "brand_name": null,
        "serving_qty": 1,
        "serving_unit": "cup",
        "serving_weight_grams": 248,
        "nf_calories": 62,
        "nf_total_fat": 2.36,
        "nf_saturated_fat": 0.65,
        "nf_cholesterol": 12.4,
        "nf_sodium": 865.52,
        "nf_total_carbohydrate": 7.32,
        "nf_dietary_fiber": 0.5,
        "nf_sugars": 0.67,
        "nf_protein": 3.15,
        "nf_potassium": 54.56,
        "nf_p": 42.16,
        "full_nutrients": [
            {
                "attr_id": 203,
                "value": 3.1496
            },
            {
                "attr_id": 204,
                "value": 2.356
            },
            {
                "attr_id": 205,
                "value": 7.316
            },
            {
                "attr_id": 207,
                "value": 2.5048
            }],
        }

【问题讨论】:

  • 我可以获得单个值,我遇到的问题是名称值对,如果 attr_id == 205,我想返回 7.316。本教程没有提供这样的示例,也许这是不可能的,我必须将所有内容存储在一个数组中并执行 if 语句遍历每一对,这似乎效率低下。

标签: java android arrays json multidimensional-array


【解决方案1】:

您的json 无效。

你可以改成这个。

{"foods": [

{
    "food_name": "chicken noodle soup",
    "brand_name": null,
    "serving_qty": 1,
    "serving_unit": "cup",
    "serving_weight_grams": 248,
    "nf_calories": 62,
    "nf_total_fat": 2.36,
    "nf_saturated_fat": 0.65,
    "nf_cholesterol": 12.4,
    "nf_sodium": 865.52,
    "nf_total_carbohydrate": 7.32,
    "nf_dietary_fiber": 0.5,
    "nf_sugars": 0.67,
    "nf_protein": 3.15,
    "nf_potassium": 54.56,
    "nf_p": 42.16,
    "full_nutrients": [
        {
            "attr_id": 203,
            "value": 3.1496
        },
        {
            "attr_id": 204,
            "value": 2.356
        },
        {
            "attr_id": 205,
            "value": 7.316
        },
        {
            "attr_id": 207,
            "value": 2.5048
        }],
    }
}

试试这个

try {
        // if your response is { },you can use JSONObject
        JSONObject jsonObject = new JSONObject(response);
        // then find the foods tag in your json data
        JSONArray foods = jsonObject.getJSONArray("foods");
        // loop for the JSONArray
        for (int i = 0; i < foods.length(); i++) {
            // getJSONObject from the index
            JSONObject jsonObject1 = foods.getJSONObject(i);
            // then get full_nutrients tag
            JSONArray full_nutrients = jsonObject1.getJSONArray("full_nutrients");
            // loop for the JSONArray
            for (int j = 0; j < full_nutrients.length(); j++) {
                // getJSONObject from the index again
                JSONObject jsonObject2 = full_nutrients.getJSONObject(i);
                // get attr_id
                String attr_id = jsonObject2.getString("attr_id");
                // get value
                String value = jsonObject2.getString("value");
            }
        }
} catch (JSONException e) {
        e.printStackTrace();
}

【讨论】:

  • 这样只会获取attr_id的值,我需要获取对应的值。所以如果 attr_id == 205 我想得到 7.316。
  • 你可以检查一下。
【解决方案2】:

试试这个:

function Double GetValue(String json_object, int attr_id) {

   Double tResult = 0;
   JSONObject reader = new JSONObject(json_object);
   // Getting JSON Array node
   JSONArray full_nutrients = reader.getJSONArray("full_nutrients");

   // looping through All full_nutrients
   for (int i = 0; i < full_nutrients.length(); i++) {
       JSONObject c = full_nutrients.getJSONObject(i);
       if (c.getInt("attr_id") == attr_id)
           tResult = c.getDouble("value");
   }
   return tResult;
}

【讨论】:

  • 谢谢,这看起来会起作用,希望有一个不需要遍历整个数组来获得单个值的解决方案。我不知道为什么服务会返回这样格式的 JSON。
猜你喜欢
  • 1970-01-01
  • 2020-08-19
  • 1970-01-01
  • 1970-01-01
  • 2013-04-29
  • 1970-01-01
  • 2021-11-04
  • 1970-01-01
  • 2011-02-04
相关资源
最近更新 更多