如何解析这个JSONArray

时间:2016-11-07 16:01:43

标签: android json

大家好我有JSONObject我很难解析它。

这里是JSON

{
    "query": {
        "count": 2,
        "created": "2016-11-07T15:29:47Z",
        "lang": "en-US",
        "results": {
            "rate": [{
                "id": "USDINR",
                "Name": "USD/INR",
                "Rate": "66.7700",
                "Date": "11/7/2016",
                "Time": "3:27pm",
                "Ask": "66.8000",
                "Bid": "66.7700"
            }, {
                "id": "USDEUR",
                "Name": "USD/EUR",
                "Rate": "0.9060",
                "Date": "11/7/2016",
                "Time": "3:29pm",
                "Ask": "0.9065",
                "Bid": "0.9060"
            }]
        }
    }
}

如上所示,有两个" Rate"我想先存储"评价"到名为Result[0]的字符串数组和第二个" Rate"到Result[1]

这就是我正在尝试的事情。

JSONObject object = new JSONObject(JSON);

JSONObject rateObject = object.getJSONObject("rate");
JSONArray rateArray = rateObject.getJSONArray("Rate");
result[0] = rateArray.getString(0);
result[1] = rateArray.getString(1);

我知道也许我这样做是愚蠢的。所以请帮帮我。

5 个答案:

答案 0 :(得分:0)

要获取数组我会尝试这个:

[["a" => "FFF", "EEE"],
 ["b" => "FFF"],
 ["c" => "EEE"]]

然后使用for来读取数组中的所有对象:

JSONObject object = new JSONObject(JSON);
JSONObject rateObject = object.getJSONObject("results");
JSONArray rateArray = rateObject.getJSONArray("rate");

答案 1 :(得分:0)

您的格式化JSON看起来像这样

{  
   "query":{  
      "count":2,
      "created":"2016-11-07T15:29:47Z",
      "lang":"en-US",
      "results":{  
         "rate":[  
            {  
               "id":"USDINR",
               "Name":"USD/INR",
               "Rate":"66.7700",
               "Date":"11/7/2016",
               "Time":"3:27pm",
               "Ask":"66.8000",
               "Bid":"66.7700"
            },
            {  
               "id":"USDEUR",
               "Name":"USD/EUR",
               "Rate":"0.9060",
               "Date":"11/7/2016",
               "Time":"3:29pm",
               "Ask":"0.9065",
               "Bid":"0.9060"
            }
         ]
      }
   }
}

要获取此{/ 1}}数组

rate

然后像这样查询JSONObject jsonObject = new JSONObject(jsonString); JSONArray rateArray = jsonObject.getJSONObject("query").getJSONObject("results").getJSONArray("rate"); 数组

rate

答案 2 :(得分:0)

安东尼, 您需要从根元素(查询)转到“rate”节点:

JSONArray rates =  object.getJSONObject("query").getJSONObject("results").getJSONArray("rate")

result[0] = rates.getJSONObject(0).get("Rate")
result[1] = rates.getJSONObject(1).get("Rate")

答案 3 :(得分:0)

你需要这样做

JSONObject jsonObj = new JSONObject(jsonString);

JSONObject queryObj = jsonObj.getJSONObject("query");
JSONObject resultObj = queryObj.getJSONObject("results");
JSONArray rateArr = resultObj.getJSONArray("rate");

result[0] = rateArr.getJSONObject(0).getString("Rate");
result[1] = rateArr.getJSONObject(1).getString("Rate");

答案 4 :(得分:-1)

试试这个:

JSONArray rateArray = new JSONObject(JSON)
    .getJSONObject("query")
    .getJSONObject("results")
    .getJSONArray("rate");
result[0] = rateArray.getJSONObject(0).getString("Rate");
result[1] = rateArray.getJSONObject(1).getString("Rate");