Voir l'exemple ci-dessous.
La chaîne de réponse est comme ce
String jsonStr = '{"menu": {' +
'"id": "file",' +
'"value": "File",' +
'"popup": {' +
'"menuitem": [' +
'{"value": "New", "onclick": "CreateNewDoc()"},' +
'{"value": "Open", "onclick": "OpenDoc()"},' +
'{"value": "Close", "onclick": "CloseDoc()"}' +
']' +
'}' +
'}}';
utiliser le code ci-dessous pour analyser la chaîne JSON
// grabbing the menu object
JSONObject jsonObf=new JSONObject(jsonStr);
JSONObject menu = jsonObj.getJSONObject("menu");
// these 2 are strings
String id = menu.getString("id");
String value = menu.getString("value");
// the popop is another JSON object
JSONObject popup = menu.getJSONObject("popup");
// using JSONArray to grab the menuitems from under popop
JSONArray menuitemArr = popupObject.getJSONArray("menuitem");
// lets loop through the JSONArray and get all the items
for (int i = 0; i < menuitemArr.length(); i++) {
// printing the values to the logcat
Log.v(menuitemArr.getJSONObject(i).getString("value").toString());
Log.v(menuitemArr.getJSONObject(i).getString("onclick");
}
Dans votre cas, faites-les simples modifications pour obtenir la solution.
Avez-vous reçu des informations/de l'aide de Google? –
S'il vous plaît regardez toutes les questions dans la section "Connexes" à la droite de cette page. – Mat