0
{"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

  • Name
  • Number of cases
  • Hyperlink
  • description
  • Maptip
  • xy
1
  • Have you tried anything? Commented Dec 8, 2013 at 9:29

3 Answers 3

1
{ // 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
Sign up to request clarification or add additional context in comments.

7 Comments

This will give JSONException, because "NAME" and other keys are missing from JSON arrays first index object.
@amitsingh try it and then comment again that is why index starts from 1 see for loop
With try-catch block, you can stop your app from crashing, but you cannot parse your jason like this.
@amitsingh did you try before posting the comment
then what about "FeatCount" key ??
|
0

Use this

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.