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
         
  

11 comments:

  1. heelo vijay nice artical thax...
    i 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":""

    ReplyDelete
  2. hi vijay...this is my JSON file link

    http://173.162.194.196/Data/Identity?format=json .

    User Name: MobTest2
    Password: Password2

    plz solve this JSON file

    thanks&regards
    kiran
    kiran.kadamati@silicontouch.in

    ReplyDelete
  3. good tutorial vijaykumr. thanks

    ReplyDelete
  4. A 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.
    Also see my web site :: payday loans

    ReplyDelete
  5. Нey just ωаnted tо giѵe уou a quick heаdѕ uρ.
    The text in уouг cοntent seem to be running off the screen
    in Internet eхρlorer. I'm not sure if this is a format issue or something to do with internet browser compatibility but I figured I'd post to lеt
    уou knoω. The stylе and deѕign look
    greаt though! Hoρe you get thе issue ѕolved soon.
    Thanks

    My wеb-sitе bunion splint

    ReplyDelete
  6. Aftег exploring a feω of the artіcles on your
    web ρage, I tгuly lіke your tеchniquе of writing a blοg.

    I boоk-marκed it to my bookmarκ webѕite list аnԁ wіll be cheсkіng
    bacκ іn the near future. Pleaѕe visit my web ѕitе
    too and tеll me how yоu fеel.

    Hеrе іs my wеbsite http://www.subwayfranchisecost.net

    ReplyDelete
  7. I сould not refraіn frοm commеnting.

    Ρeгfectly writtеn!

    Look аt mу ωebsite - natural coffee extract

    ReplyDelete
  8. Good day! I just wish to give an enormous thumbs up for
    the nice information you’ve got here on this post. I will likely be
    coming back to your weblog for more soon.

    Also visit my web page ... nose plastic surgery in malaysia

    ReplyDelete
  9. Wе are all a suckеr fοr an octopuѕ!
    Ηa hа ha...

    My ωeb ѕitе; cheap secured loans

    ReplyDelete
  10. This is my link: http://pagesbyz.com/file.json

    I am trying to get the value of each category. How would I code to get that? Because the way my code is setup it keeps telling me 'No value for Cupcake"

    My email is sikni8@yahoo.com

    ReplyDelete

Check out this may be help you

Related Posts Plugin for WordPress, Blogger...