I am writing an android code to parse a json array. I am not sure how it is to be done. I have however tried to parse the array.
These are the following things that I need to do:
I want to store the "status" in a variable.
Also, I want to store "ButtonImageUrl", "ImageUrl", "Title", "Url" iteratively in a string.
If anyone could guide me how should I proceed it would be very helpful.
{
"first": {
"message": "",
"status": "Success"
},
"second": [
{
"ButtonImageUrl": "string1",
"ImageUrl": "string2",
"Title": "string3",
"Url": "string4"
},
{
"ButtonImageUrl": "string1",
"ImageUrl": "string2",
"Title": "string3",
"Url": "string4"
},
{
"ButtonImageUrl": "string1",
"ImageUrl": "string2",
"Title": "string3",
"Url": "string4"
},
{
"ButtonImageUrl": "string1",
"ImageUrl": "string2",
"Title": "string3",
"Url": "string4"
},
{
"ButtonImageUrl": "string1",
"ImageUrl": "string2",
"Title": "string3",
"Url": "string4"
},
{
"ButtonImageUrl": "string1",
"ImageUrl": "string2",
"Title": "string3",
"Url": "string4"
},
{
"ButtonImageUrl": "string1",
"ImageUrl": "string2",
"Title": "string3",
"Url": "string4"
},
{
"ButtonImageUrl": "string1",
"ImageUrl": "string2",
"Title": "string3",
"Url": "string4"
},
{
"ButtonImageUrl": "string1",
"ImageUrl": "string2",
"Title": "string3",
"Url": "string4"
},
{
"ButtonImageUrl": "string1",
"ImageUrl": "string2",
"Title": "string3",
"Url": "string4"
},
{
"ButtonImageUrl": "string1",
"ImageUrl": "string2",
"Title": "string3",
"Url": "string4"
},
{
"ButtonImageUrl": "string1",
"ImageUrl": "string2",
"Title": "string3",
"Url": "string4"
},
{
"ButtonImageUrl": "string1",
"ImageUrl": "string2",
"Title": "string3",
"Url": "string4"
},
{
"ButtonImageUrl": "string1",
"ImageUrl": "string2",
"Title": "string3",
"Url": "string4"
}
]
}
Code:
private void jsonrequest2()
{
request = new JsonArrayRequest(JSON_URL2, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try
{
jsonArray = null;
jsonArray = response.getJSONArray("second");
//jsonObject = response.getJSONArray("second");
} catch (JSONException e) {
e.printStackTrace();
}
for(int i = 0 ; i<jsonArray.length();i++)
{
try
{
ExampleItem exampleItem = new ExampleItem();
exampleItem.setmText1(jsonArray.getJSONObject(i).getString("Title"));
exampleItem.setmText2(jsonArray.getJSONObject(i).getString("Title"));
exampleItem.setImageUrl(jsonArray.getJSONObject(i).getString("ButtonImageUrl"));
//jsonArray.getJSONObject(i).getString("Title")
exampleList.add(exampleItem);
}
catch (JSONException e)
{
e.printStackTrace();
}
}
setuprecyclerview(exampleList);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue = Volley.newRequestQueue(ProductPage1.this);
requestQueue.add(request);
}
responseis not aJSONArray, it is aJSONObjectJsonArrayRequest