Android Studio Volley JSON REST API Parsing - android

Writing Android Application, Need to Parse JSON Data which is nested
Below is Actual data received from http.
Currently using Volley,
JSONArray dataJSONArray = response.getJSONArray("data");
Need example code to parse and display data from below mentioned JSON data
{"data":[{"counting_area_id":3,"name":"Utilization","parking_area_id":1, "free":3,"total":200,"location_latitude":null,"location_longitude":null,"places":10,
"children":[{"counting_area_id":1,"name":"Basement 1","parking_area_id":1, "free":0,"total":116,"location_latitude":null,"location_longitude":null,"places":0,
"children":[]},{"counting_area_id":73,"name":"Basement 2","parking_area_id":1, "free":3,"total":121,"location_latitude":null,"location_longitude":null,"places":3,
"children":[]}]}]}

You need to passed your data to JSONArray and parse through it using for loop ,like below code :
JSONArray jsonArray = response.getJSONArray("data");//getting array
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonobject= jsonArray.getJSONObject(i);//getting first element
String id= jsonobject.getString("counting_area_id");//value of counting_area_id ,get all value in same way i.e location,places etc.
System.out.println(id);
JSONArray jsonObject1= object.getJSONArray("children"); //getting children array
for (int j = 0; j < jsonObject1.length(); j++) {
JSONObject object1 = jsonObject1.getJSONObject(j);
String id= object1.getString("counting_area_id");//same as before
}
}

Related

parsing nested JSON array using volley library in android

How can I parse nested JSON array using volley library?
My JSON data structure screenshot.
https://prnt.sc/pbaea5
I need to perse score value.
JSONArray jsonArray = response.getJSONArray("matches");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObjectMatchs = jsonArray.getJSONObject(i);
// bat_team node is JSON Object
JSONObject bat_teamData = jsonObjectMatchs.getJSONObject("bat_team");
JSONArray jsonArrayInnings = bat_teamData.getJSONArray("innings");
JSONObject jsonObjectInnings = jsonArrayInnings.getJSONObject(i);
String bat_team_score = jsonObjectInnings.getString("score");
}
You need to iterate further on
jsonArrayInnings using another loop but in your code you are using i of parent loop.
It will not work properly.

Parsing json array with no arrayobject name in android

I want to parse the following data, everything is done but i'm confused at this last stage:
["ABC","DEF","GHI"]
How can I parse this type of json data?
Do it like this
JSONArray arr = new JSONArray(data); // assuming your json array is store in data
for(int i = 0; i < arr.length(); i++)
{
String str = arr.getString(i); // will return one string at a time
}

error while parsing an jsonarray

I am making a webservice call and the respond is in JSON.
The content which i get, is a JSONArray and looks like this:
{"jsonrpc":"2.0","id":"req-002","result":[
{"id":125043,"date":20110117,"startTime":800,"endTime":850,
"kl":[{"id":71}],"te":[{"id":23}],"su":[{"id":13}],"ro":[{"id":1}]},
{"id":125127,"date":20110117,"startTime":1055,"endTime":1145,
"kl":[{"id":71}],"te":[{"id":41}],"su":[{"id":19}],"ro":[{"id":31}]},
]}
Now i am trying to get the objects in the array, but i am only able to fetch the first array: for example i cant get the first "kl" array but i am not able to get the second one.
It always give me the error :
org.json.JSONException: Index 1 out of range [0..1)
This is what i have tried:
JSONObject jsonResult = new JSONObject(s);
// Get the result object
JSONArray arr = jsonResult.getJSONArray("result");
Log.d("Arraylänge", String.valueOf(arr.length()));
for(int i=0; i<arr.length(); i++){
JSONObject c = arr.getJSONObject(i);
anfangStunde[i] = c.getString("startTime");
endeStunde[i] = c.getString("endTime");
JSONArray klArr = c.getJSONArray("kl");
for(int j=0; i<klArr.length(); j++)
{
JSONObject k = klArr.getJSONObject(j);
klassenID[j] = k.getString("id");
}
You have a typo for(int j=0; **i**<klArr.length(); j++)

How to parse Multiple JSON Objects and arrays from a url in android? i am using a example i want it in that example,

I want below process on with out editing on server side
I followed this example
but over there he is calling data with one json object from a url
called "worldpopulation",..
but i have a Json data which is having lot of Json objects and arrays,.. like below code
{ "getting data":"ok"
"Todays":population
{"results"{[
"worldpopulation":
[
{
"rank":1,"country":"China",
"population":"1,354,040,000",
"flag":["http://www.androidbegin.com/tutorial/flag/china.png"]
},
{
"population":{
[
"countrypulation" {
"rank":2,"country":"India",
"population":"1,210,193,422",
"flag":["http://www.androidbegin.com/tutorial/flag/india.png"]
},
like that i have a lot of data i want this data in that above example. Without changing any thing on server side,.. or Restful service,..
thank you
response
JSONObject JObject = new JSONObject(response);
String getting_data = JObject.getstring("getting data");
String Todays= JObject.getstring("Todays");
JsonArray results =JObject.getjsonarray("results");
for(int i = 0 ; i < results.length(); i++){
JsonObject resultsobject =results.getjsonobject(i);
JsonArray worldpopulation
=resultsobject.getjsonarray("worldpopulation");
for(int j = 0 ; j < worldpopulation.length(); j++){
JsonObject worldpopulationobject =worldpopulation.getjsonobject(j);
JsonObject emptyobject =worldpopulationobject.getjsonobject("");
String rank=emptyobject .getstring("rank");
String population=emptyobject .getstring("population");
JsonArray flagarray =countrypulation.getjsonarray(flag);
for(int n = 0 ; n < flagarray.length(); n++){
JsonObject flagarrayobject =flag1array.getjsonobject(n);
String flag=flag1arrayobject .getstring("flag");
}
JsonObject populationobject
=worldpopulationobject.getjsonobject("population");
JsonArray emptyarray =JObject.getjsonarray("");
for(int k = 0 ; k < emptyarray.length(); k++){
JsonObject emptyarrayobject =emptyarray.getjsonobject(k);
JsonObject
countrypulation=emptyarrayobject.getjsonobject("countrypulation");
String rank1=countrypulation .getstring("rank");
String country=countrypulation .getstring("country");
String population1=countrypulation .getstring("population");
JsonArray flag1array =countrypulation.getjsonarray(flag);
for(int m = 0 ; m < flag1array.length(); m++){
JsonObject flag1arrayobject =flag1array.getjsonobject(m);
String flag1=flag1arrayobject .getstring("flag");
}
}
}
}
You parse
{
...
}
as JSONObject and
[
...
]
as JSONArray. JSONArray contains your objects. From the JSONObject you can retrieve the property values.
JSONArray response = new JSONArray('your json string');
for (int i = 0; i < response.length(); i++) {
response.getJSONObject(i).getString("property"));
}
BTW your json is invalid, event if you close the missing brackets. Check it here http://jsonlint.com/

Json data extracting in android

i want to extract the given json arry in android,
[{"Outofserviceday":{"outofservice":"2013-02-22"}},
{"Outofserviceday":{"outofservice":"2013-02-27"}},
{"Outofserviceday":{"outofservice":"2013-02-28"}}]
i have the code for extracting the json data like given below
[{"Requestcard":{"id":"994","userprofile_id":"14","userprofile_name":"Syed
Imran","company_name":"DLF Akruti Park, Hinjewadi, Pune,
Maharashtra","sex":"male","travel_date":"2013-02-12"}}]
in this case we can retrive the json boject using the code
JSONObject menuObject = json_data.getJSONObject("Requestcard");
and retrieve each element by
requestid= menuObject.getString("id");
But in the first case how we identify each Outofserviceday in the json array ? and How extract each data ???
you can do something like below, can create jsonArray from string json data and then can extracts json objects in a loop or so.
String json =" [{\"Outofserviceday\":{\"outofservice\":\"2013-02-22\"}}]"; //json-data which is basically a json array
JSONArray jArray = new JSONArray(json); / creating an jsonarray
for (int i = 0; i < jArray.length(); i++) {
// you can have jsonObject from json array here in the loop
}
Try this:
JSONObject json = new JSONObject(result);
JSONArray json1= json.getJSONArray("data");
if (json1.length()!=0) {
for (int i = 0; i < json1.length(); i++) {
String name = json1.getJSONObject(i).getString("name");
}
}
With the help of below code you can retrieve the value of outofservice
JSONArray jArray = new JSONArray(your data);
for (int i = 0; i < jArray.length(); i++) {
JSONObject jOutOfServiceDay = jArray.getJSONObject(i);
JSONObject jobj = jOutOfServiceDay.getJSONObject("Outofserviceday");
Log.i("Required data is:", "" + jobj.getString("outofservice"));
}

Categories

Resources