Android JSONParsing Tutorial:
JSON is the best alternative to XML for storing data in files. It is easy to parse and access data stored in JSON format.
JSON Structure:
i am using example for json google route direction. below structure of json.
JSON is the best alternative to XML for storing data in files. It is easy to parse and access data stored in JSON format.
JSON Structure:
i am using example for json google route direction. below structure of json.
u can get JSON data from this api: http://maps.googleapis.com/maps/api/directions/json?origin=Tamil%20ndau,Salem&destination=Tamil%20nadu,%20salem®ion=in&sensor=false
{
"routes" : [----- first JsonArray
{--- First json object
"bounds" : {--- Second JsonObject
"northeast" : {--- Third JsonObject
"lat" : 11.664450,
"lng" : 78.14620000000001
},
"southwest" : {
"lat" : 11.664450,
"lng" : 78.14620000000001
}
},
"legs" : [--- Second JsonArray
{
"distance" : {
"text" : "1 m",
"value" : 0
},
"duration" : {
"text" : "1 min",
"value" : 0
},
"end_address" : "Salem, Tamil Nadu, India",
"end_location" : {
"lat" : 11.664450,
"lng" : 78.14620000000001
},
"start_address" : "Salem, Tamil Nadu, India",
"start_location" : {
"lat" : 11.664450,
"lng" : 78.14620000000001
},
"steps" : [---- Third JSON Array
{
"distance" : {
"text" : "1 m",
"value" : 0
},
"duration" : {
"text" : "1 min",
"value" : 0
},
"end_location" : {
"lat" : 11.664450,
"lng" : 78.14620000000001
},
"html_instructions" : "Head \u003cb\u003eeast\u003c/b
\u003e on \u003cb
\u003eOmalur Main Road\u003c/b\u003e",
"polyline" : {
"points" : "yeefAw|}{M"
},
"start_location" : {
"lat" : 11.664450,
"lng" : 78.14620000000001
},
"travel_mode" : "DRIVING"
}
],
"via_waypoint" : []
}
],
"overview_polyline" : {
"points" : "yeefAw|}{M"
},
"summary" : "Omalur Main Road",
"warnings" : [],
"waypoint_order" : []
}
],
"status" : "OK"
}
This JSON Structure for Multiple array of values
PROBLEM:
How to read multible JsonArray .
SOLUTIONS:
Before see the below code. please check above the JSON Structure.
String loginUrl = "http://maps.googleapis.com/maps/api/directions/json?
origin=Tamil%20ndau,Salem&destination=Tamil%20nadu,%20
salem®ion=in&sensor=false";
try{
HttpGet request = new HttpGet(loginUrl);
HttpResponse response = httpClient.execute(request);
entityResponse = response.getEntity();
result = EntityUtils.toString(entityResponse);
JSONArray array = object.getJSONArray("routes");
---Parsing first jsonArray
JSONObject routes = array.getJSONObject(0);
---(parsing first jsonarray object) String bounds= routes.getString("bounds");
-- parsing second josn Array JSONArray legs = routes.getJSONArray("legs");
--- (parsing second jsonarray object)
JSONObject steps = legs.getJSONObject(0);
String distance= routes.getString("distance");
--- parsing third json Array JSONArray legs1 = steps.getJSONArray("steps"); for(int i = 0; i < legs1.length(); i++){ JSONObject steps1 = legs1.getJSONObject(i);
--parsing third jsonarray objecct String htMlVale = steps1.getString("html_instructions").toString();
-- parsing inside third jsonarray of jsonarray JSONObject distance = steps1.getJSONObject("distance"); String sDistance = distance.getString("text");()
}
}
more question ask me..... or any help about JSON
heelo vijay nice artical thax...
ReplyDeletei have complex json file
{"categories":{"data":[{"CATEGORY_ID":"1","CATEGORY_NAME":"Fruits","CATEGORY_PRIORITY":"1","CATEGORY_PARENT_ID":"0","CATEGORY_DESCRIPTION":"Fruits","CATEGORY_IMAGE":"","NOF_SUBCATEGORIES":"2","NOF_PRODUCTS":5,"SubCategories":[{"CATEGORY_ID":"3","CATEGORY_NAME":"Apple","CATEGORY_PRIORITY":"2","CATEGORY_PARENT_ID":"1","CATEGORY_DESCRIPTION":null,"CATEGORY_IMAGE":"globallr.com\/test\/ecomveggibazar\/img\/apple.jpeg","NOF_SUBCATEGORIES":"0","NOF_PRODUCTS":""
hi
ReplyDeletehi vijay...this is my JSON file link
ReplyDeletehttp://173.162.194.196/Data/Identity?format=json .
User Name: MobTest2
Password: Password2
plz solve this JSON file
thanks®ards
kiran
kiran.kadamati@silicontouch.in
good tutorial vijaykumr. thanks
ReplyDeletethanks
ReplyDeletegood
ReplyDeletegood
ReplyDeleteA paper trading account acts exactly like a genuine trading account however the cash isn't real though the results can be had you used real cash payday loans military loans for bad credit borrowers: how to enjoy the full benefits.
ReplyDeleteAlso see my web site :: payday loans