1

I need to parse Json in Android, as a newbie in Json as well as in Android I am unable to do so here is the json String:

[
    {
        "chapter": "1. General",
        "lessons": [
            {
                "lesson": "1.1 "
            },
            {
                "lesson": "1.2"
            },
            {
                "lesson": "1.3"
            }
        ]
    },
    {
        "chapter": "2.emergencies"
    }
]

Here I just want to get the lessons array data. So any help will really be appreciated. Thanks

Selvin
  • 6,598
  • 3
  • 37
  • 43
Jai Android
  • 2,131
  • 10
  • 38
  • 55
  • http://stackoverflow.com/questions/2818697/sending-and-parsing-json-in-android – Caner Aug 10 '11 at 11:49
  • put json string here => http://jsonviewer.stack.hu/ click view ... you will see tree ... [] means array, {} means object/dictionary – Selvin Aug 10 '11 at 11:54

3 Answers3

0

Use JSONObject and/or JSONArray. They can be created directly from strings, eg:

JSONObject json = new JSONObject(string);
Cesar
  • 2,027
  • 2
  • 18
  • 29
0

Android includes a JSON parser: http://developer.android.com/reference/org/json/package-summary.html

String json = "{ ... }";
JSONObject obj = new JSONObject(json);
JSONArray array = obj.getJSONArray("lessions");
for (int i = 0; i < array.length(); i++) {
    String lession = array.getJSONObject(i).getString("lession");
}
devconsole
  • 7,875
  • 1
  • 34
  • 42
0
JSONObject jObject = new JSONObject(jsonString);
JSONObject chapObject = jObject.getJSONObject("chapter");
Log.d("Chapt", chapObject.getString("chapter"));
JSONArray lessonArray = popupObject.getJSONArray("lessons");
for (int i = 0; i < 3; i++) {
Log.d("Name", lessonArray.getJSONObject(i).getString("lesson").toString());

Log.d("Value", lessonArray.getJSONObject(i).getString("onclick").toString());   
}      
JSONObject chap2Object = jObject.getJSONObject("chapter");
Log.d("Chapt2", chapObject.getString("chapter"));
Hanry
  • 5,481
  • 2
  • 40
  • 53