2010-11-22 15 views
29

Sto provando a leggere un array JSON. Ecco il mio codice.Leggere un array Json in Android

 JSONArray jArray = new JSONArray(jsonString); 

     System.out.println("*****JARRAY*****"+jArray.length()); 
     for(int i=0;i<jArray.length();i++){ 


       JSONObject json_data = jArray.getJSONObject(i); 
       Log.i("log_tag","_id"+json_data.getInt("account")+ 
         ", mall_name"+json_data.getString("name")+ 
         ", location"+json_data.getString("number")+ 
         ", telephone"+json_data.getString("url")+ 
         ",----"+json_data.getString("balance")+ 
         ",----"+json_data.getString("credit")+ 
         ",----"+json_data.getString("displayName") 
       ); 

     } 

E il mio campione sintassi file JSON è la seguente,

{ 
    "list": [ 
     { 
      "account": 1, 
      "name": "card", 
      "number": "xxxxx xxxx xxxx 2002", 
      "url": "http://www.google.com", 
      "balance": 1.0, 
      "credit": 1.0, 
      "displayName": "hsbc bank" 
     }, 
     { 
      "account": 2, 
      "name": "card2", 
      "number": "xxxxx xxxx xxxx 3003", 
      "url": "http://www.google.com", 
      "balance": 2.0, 
      "credit": 2.0, 
      "displayName": "nsb bank" 
     } 
    ], 
    "count": 2 
} 

Ha una parentesi graffa di fronte. Quando provo ad eseguire questo blocco di codice dà un errore che dice

Un JSONArray testo deve iniziare con '[' a carattere 1 di ....

Qualcuno ha riscontrato un problema come questo ? Qualsiasi aiuto sarebbe molto apprezzato. Mostrami un blocco di codice di esempio, se possibile. Grazie in anticipo.

risposta

62

Un JSON oggetto inizia con una { e termina con un } mentre un array JSON inizia con una [ e termina con un ].

Nel tuo caso, modificare il codice per avere un JSONObject invece.

JSONObject json = new JSONObject(jsonString); 
JSONArray jArray = json.getJSONArray("list"); 

System.out.println("*****JARRAY*****" + jArray.length()); 

for(int i=0; i<jArray.length(); i++){ 
    JSONObject json_data = jArray.getJSONObject(i); 

    Log.i("log_tag", "_id" + json_data.getInt("account") + 
     ", mall_name" + json_data.getString("name") + 
     ", location" + json_data.getString("number") + 
     ", telephone" + json_data.getString("url") + 
     ",----" + json_data.getString("balance") + 
     ",----" + json_data.getString("credit") + 
     ",----" + json_data.getString("displayName") 
    ); 
} 
+0

Grande !! Funziona grazie mille – nala4ever

+2

Grande, felice di essere stato di aiuto. Non dimenticare di ** accettare ** una risposta! :-) –

+0

tks man funziona per me –

7

Innanzitutto è necessario creare un JSONObject per ottenere l'Array dai, qualcosa di simile a questo dovrebbe funzionare:

JSONObject jsonObject = new JSONObject(jsonString); 

JSONArray jArray = jsonObject.getJSONArray("list"); 
+0

grazie matt. Funziona – nala4ever

1

String result = js.getString ("Result");

    JSONArray js2 = new JSONArray(result); 
        for (int i = 0; i < js2.length(); i++) { 
         JSONObject js3 = js2.getJSONObject(i); 
         categoriescity.add(js3.getString("Title")); 
        } 
+0

Grazie mille, molto, Razid Mirazimi. Mi hai salvato la giornata. – iSofia