Saturday, February 11, 2012

Android JSONParsing Tutorial

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.
{
   "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&region=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
         
  

Check out this may be help you

Related Posts Plugin for WordPress, Blogger...