2010-11-22 8 views
29

Ich versuche, ein JSON-Array zu lesen. Hier ist mein Code.Ein JSON-Array in Android lesen

 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") 
       ); 

     } 

Und meine Probe JSON-Dateien Syntax ist wie folgt,

{ 
    "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 
} 

Es hat eine geschweifte Klammer vor. Wenn ich versuche, diesen Codeblock auszuführen gibt es eine Fehlermeldung,

Text A JSONArray muss beginnen mit ‚[‘ an Charakter 1 von ....

Hat jemand ein Problem wie dieses angetroffen ? Jede Hilfe würde sehr geschätzt werden. Bitte zeigen Sie mir einen Beispielcodeblock wenn möglich. Vielen Dank im Voraus.

Antwort

62

Ein JSON-Objekt beginnt mit einem { und endet mit einem } während ein JSON-Array mit einem [ beginnt und endet mit einem ].

In Ihrem Fall ändern Sie Ihren Code, um stattdessen eine JSONObject zu haben.

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

Großartig !! Es funktioniert Vielen Dank – nala4ever

+2

Großartig, froh gewesen zu sein von Hilfe. Vergessen Sie nicht, ** eine Antwort zu akzeptieren **! :-) –

+0

tks man das funktioniert für mich –

7

Sie müssen zuerst eine JSONObject erstellen aus dem Array zu erhalten, sollte in etwa so funktionieren:

JSONObject jsonObject = new JSONObject(jsonString); 

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

danke matt. Es funktioniert – nala4ever

1

Zeichenfolge 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

Vielen Dank, sehr, Rashid Mirazimi. Du hast meinen Tag gerettet. – iSofia