0

Possible Duplicate:
Sending and Parsing JSON in Android

{
    "title": " '''Sachin Ramesh Tendulkar''' (born 24 April 1973) is an Indian cricketer widely regarded as one of the greatest batsmen in the history of cricket. ",
    "sub": {
        "sub0": {
            "name": "Cricket",
            "importance": "1"
        },
        "sub1": {
            "name": "Wisden Cricketers of the Year",
            "importance": "1"
        },
        "sub2": {
            "name": "Marathi people",
            "importance": "1"
        },

    },
    "heading": {
        "p1": {
            "point": " Tendulkar . "
        },
        "p2": {
            "point": " He."
        },
        "p3": {
            "point": " 2009. "
        },
    }
}
Community
  • 1
  • 1
anoop
  • 782
  • 4
  • 12
  • 35

2 Answers2

2

whenever get result from web it returns you a string and you have to convert it to JsonObject like this

JSONObject json = new JSONObject (response);

by this you will get whole string get converted to json then you can retrive all values from json using JsonObject class methods like this

String player name = json.getString("title");

You will get this result

Sachin Ramesh Tendulkar''' (born 24 April 1973) is an Indian cricketer widely regarded as one of the greatest batsmen in the history of cricket.

Sachin Gurnani
  • 4,421
  • 9
  • 41
  • 48
1

In this case you can use the keys method of the JSONObject class. It will basically returns an Iterator of the keys, that you can then iterate to get and put the values in a map:

try {
        JSONObject jsonObject = new JSONObject(theJsonString);
        Iterator keys = jsonObject.keys();
        Map<String, String> map = new HashMap<String, String>();
        while (keys.hasNext()) {
            String key = (String) keys.next();
            map.put(key, jsonObject.getString(key));
        }
        System.out.println(map);// this map will contain your json stuff
    } catch (JSONException e) {
        e.printStackTrace();
    }

Refer: Parsing Json String, Parse Json String in Android

Community
  • 1
  • 1
Ponmalar
  • 6,814
  • 10
  • 48
  • 77