Json data extracting in android - 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"));
}

Related

Android Studio Volley JSON REST API Parsing

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
}
}

JSON Array Displaying only last element Android TextView

I am trying to list the JSON array and display in the TextViews, but somehow it shows only last element of the JSON array, please suggest the solution, here is my code.
JSONObject jObj = new JSONObject(response);
JSONObject jsonData = jObj.getJSONObject("data");
JSONArray jsonarr = jsonData.getJSONArray("order_status_list");
for (int i1 = 0; i1 < jsonarr.length(); i1++) {
JSONObject c1 = jsonarr.getJSONObject(i1);
txt_order_datetime.setText(c1.optString("status_datetime"));
txt_order_status.setText(c1.optString("order_status_value"));
txt_order_updatedby.setText(c1.optString("status_updated_by_user"));
}
Thanks
As suggested by #sector11, you can use
txt_order_datetime.append(c1.optString("status_datetime"));

How to read this JSON

String json =
[
{"a":{"aa":"string","ab":"string"} },
{"b":{"ba":"string","bb":"string"} }
]
I am trying to parse this data using JsonObject.
When i use this code :
JSONStringer Js= new JSONStringer(json);
Log.d("json", ""+Js);
it gives me this : (the first line of json but i want all data)
{"a":{"aa":"string","ab":"string"} }
How can I read this with Android ?
It is a JsonArray not a simple JsonObject
try this:
JSONArray a = new JSONArray(json);
for (int i = 0; i < a.length(); i++) {
JSONObject row = a.getJSONObject(i);
Log.d("json", ""+row);
}
Actually, this JSON is kind of weird, as it wraps the array objects into one additional object each ("a" and "b"), which will make it hard to parse. It should look like this:
String json = [ {"name":"a","aa":"string","ab":"string"},
{"name":"b","ba":"string","bb":"string"} ]
And then you would parse it like this:
JSONArray root = new JSONArray(jsonString); //this is the above string
for(int i = 0; i < root.length(); i++){
JSONObject current = root.getJSONObject(i);
//Do the parsing here
}

How to split this kind of data in android

Here is the
string one =[{"ID":5,"Name":"Sai"}]
how i get only id and name from this string
Matcher matcher = Pattern.compile("\\[([^\\]]+)").matcher(one);
List<String> tags = new ArrayList<String>();
int pos = -1;
while (matcher.find(pos+1)){
pos = matcher.start();
tags.add(matcher.group(1));
}
System.out.println("getting data"+tags);
i tried this but it didn't work
List<String> ls = new ArrayList<String>(one);
JSONArray array = new JSONArray();
for(int i = 0; i< array.length(); i++){
JSONObject obj = array.getJSONObject(i);
ls.add(obj.getString("Name"));
}
It's JSON format and it can very easily be read in Android. Here is the sample code:
JSONArray array = new JSONArray(one);
int length = array.length();
for(int i=0;i< length; i++)
{
JSONObject temp = array.getJSONObject(i);
System.out.println(temp.getString("ID"));
System.out.println(temp.getString("Name"));
}
This format of data is called JSON.
Have a look at Go to http://json.org/, scroll to (almost) the end, click on one of the many Java libraries listed.
First of all, your string initialization is wrong.
Wrong:
string one =[{"ID":5,"Name":"Sai"}]
Correct:
String one ="[{\"ID\":5,\"Name\":\"Sai\"}]";
Second, its a JSON formatted data so you can parse it using JSONArray and JSONObject classes, instead of creating any pattern.
Now, in your case its JSONObject inside JSONArray so initially create an object of JSONArray using your string.
For example:
JSONArray arrayJSON = new JSONArray(one); // 'one' is your JSON String
for(int i=0; i<arrayJSON.length(); i++) {
JSONObject objJson = arrayJSON.getJSONObject(i);
String ID = objJson.getString("ID");
.....
.....
// same way you can fetch/parse any string/value from JSONObject/JSONArray
}
it is a json formate Date
use JsonObject class to parse this data
tutorial this
JSONArray jsonArray = new JSONArray("[{\"ID\":5,\"Name\":\"Sai\"}]");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
System.out.println(object.getString("ID"));
System.out.println(object.getString("Name"));
}

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/

Categories

Resources