Abdul.Moqueet Abdul.Moqueet - 26 days ago 8
Android Question

how to parse this JSONArray

Hi guys i have this

JSONObject
I'm having hard time to parse it.

Here's the
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"
}]
}
}
}


As you can see above there are two "Rate" I want to store first "Rate" to a String array called
Result[0]
and second "Rate" to
Result[1]
.

Here what I'm trying.

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);


I know maybe I'm doing this like stupid. So please help me out.

Answer

Try this:

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");
Comments