2011-11-11 7 views
-4
"carMake": { 
      "Tata": [ 
       "FIAT", 
       "INDICA VISTA", 
       "INDIGO XL" 
      ], 
      "Hyndai": [ 
       "SANTRO Xing", 
       "I10", 
       "I20", 
       "ACCENT", 
       "SONATA" 
      ] 
     }, 

J'ai juste besoin d'analyser cette partie dans ma réponse. J'ai essayé avec la carte mais je ne l'obtiens pas résolu.Comment analyser le JSON dans Android?

+2

Avez-vous reçu des informations/de l'aide de Google? –

+2

S'il vous plaît regardez toutes les questions dans la section "Connexes" à la droite de cette page. – Mat

Répondre

0

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.

Questions connexes