user2511316
user2511316

Reputation: 15

Getting JSON values using android

{"SrchResults":[{"FeatCount":"53"},{"NAME":"Dengue_Cluster","Number of cases":"10","HYPERLINK":"http://www.dengue.gov.sg/cms/ehd/20131647.jpg ","DESCRIPTION":"Ang Mo Kio Ave 3 (Blk 126, 127, 128) / Ang Mo Kio Ave 6 (Blk 124, 125)","MAPTIP":"Dengue_Cluster","SYMBOLCOLOR":"#E600A9","XY":"29342.6037999997,39300.7427999992"}{"NAME":"Dengue_Cluster","Number of cases":"11","HYPERLINK":"http://www.dengue.gov.sg/cms/ehd/20131578.jpg ","DESCRIPTION":"Guillemard Rd / Lor 28 Geylang / Lor 30 Geylang","MAPTIP":"Dengue_Cluster","SYMBOLCOLOR":"#E600A9","XY":"33644.5215999996,32574.9587999992"}]}

How do i extract the above data? I wanted to get the

Upvotes: 0

Views: 72

Answers (3)

Raghunandan
Raghunandan

Reputation: 133560

{ // json object node
    "SrchResults": [ // jsona array
        {
            "FeatCount": "53"
        },
        {             // json object node
            "NAME": "Dengue_Cluster",    // string
            "Number of cases": "10",
            "HYPERLINK": "http://www.dengue.gov.sg/cms/ehd/20131647.jpg ",
            "DESCRIPTION": "Ang Mo Kio Ave 3 (Blk 126, 127, 128) / Ang Mo Kio Ave 6 (Blk 124, 125)",
            "MAPTIP": "Dengue_Cluster",
            "SYMBOLCOLOR": "#E600A9",
            "XY": "29342.6037999997,39300.7427999992"
        },
        {
            "NAME": "Dengue_Cluster",
            "Number of cases": "11",
            "HYPERLINK": "http://www.dengue.gov.sg/cms/ehd/20131578.jpg ",
            "DESCRIPTION": "Guillemard Rd / Lor 28 Geylang / Lor 30 Geylang",
            "MAPTIP": "Dengue_Cluster",
            "SYMBOLCOLOR": "#E600A9",
            "XY": "33644.5215999996,32574.9587999992"
        }
    ]
}

Toparse

      try
      {
          JSONObject jObj = new JSONObject("My Json string");
          JSONArray jr = jObj.getJSONArray("SrchResults");
          JSONObject json = (JSONObject) jr.get(0);
          String key = json.getString("FeatCount");
          Log.i("...........",key);
          for(int i=1;i<jr.length();i++)
          {
              JSONObject jb= jr.getJSONObject(i);
              String name = jb.getString("NAME");
              String nofcase = jb.getString("Number of cases");
              Log.i("Name is....",name);
              Log.i("No of cases .........",nofcase);

          }
          }catch(Exception e)
      {
          e.printStackTrace();
      }

Log

12-08 09:53:32.212: I/...........(30147): 53
12-08 09:53:32.212: I/Name is....(30147): Dengue_Cluster
12-08 09:53:32.212: I/No of cases .........(30147): 10
12-08 09:53:32.222: I/Name is....(30147): Dengue_Cluster
12-08 09:53:32.222: I/No of cases .........(30147): 11

Upvotes: 1

Kurian Vithayathil
Kurian Vithayathil

Reputation: 837

Java has it's own JSON library although I prefer Google GSON which is more OO oriented: Google GSON

Upvotes: 0

Bahram
Bahram

Reputation: 1642

Use this

JSONObject json = new JSONObject(jsonstring);
json.getString("name");
....

Upvotes: 0

Related Questions