2013-08-13 20 views
7

come convertire correttamente questa stringa in un jsonArray?Converti stringa in JsonArray

{ 
    "myArray": [ 
    { "id": 1, 
     "att1": 14.2, 
     "att2": false }, 
    { "id": 2, 
     "att1": 13.2, 
     "att2": false }, 
    { "id": 3, 
     "att1": 13, 
     "att2": false } 
    ]} 

Un JSONArray jArray = new JSONArray(STRING_FROM_ABOVE); risultati in jArray.length = 1 La mia prima volta a entrare in contatto con JSON :)

+0

La stringa è un oggetto JSON, non un array (notare le parentesi più esterne). – Henry

risposta

10

Prova questo:

JSONObject jObject = new JSONObject(STRING_FROM_ABOVE); 
JSONArray jArray = jObject.getJSONArray("myArray"); 

Il "string_from_above" non è un array JSON, si tratta di un oggetto JSON, che contiene un attributo (myArray) che è un array JSON;)

È quindi possibile fare :

for (int i = 0; i < jArray.length(); i++) { 
     JSONObject jObj = jArray.getJSONObject(i); 
     System.out.println(i + " id : " + jObj.getInt("id")); 
     System.out.println(i + " att1 : " + jObj.getDouble("att1")); 
     System.out.println(i + " att2 : " + jObj.getBoolean("att2")); 
} 
+1

Grazie mille ... dovrei davvero guardare più vicino a JSON e alla loro sintassi :) – DieselPower

+0

Nessun problema, non dimenticare di accettare la risposta però :) – Organ

+0

Quindi non puoi dire jArray [i]; eh? – marciokoko

0

Dai un'occhiata alla JQuery, e in particolare la funzione Parse. Questo analizzerà direttamente una matrice Json in un oggetto.

+0

Jason, davvero !! – blackpanther

+0

Correzione automatica! È foneticamente corretta comunque. – PizzaTheHut

2

Durante l'analisi, aggiungere valori agli oggetti e liste di array o comunque il modo in cui si desidera utilizzarlo. In bocca al lupo.

JSONObject jo = new JSONObject(STRING_FROM_ABOVE); 
JSONArray rootArray= jo.getJSONArray("myArray"); 
int rootArrayLength=rootArray.length(); 
for(int i=0;i<rootArrayLength;i++){ 
    int id= rootArray.getJSONObject(i).getInt("id"); 
    // do same for others too and create an object 
} 
// create object and make a list 
1

solo per gettare un altro metodo nella miscela qui, vorrei consigliare di dare un'occhiata a Gson. Gson è una libreria che semplifica la serializzazione e la deserializzazione dagli oggetti Java. Ad esempio, con la stringa, si potrebbe fare questo:

// Declare these somewhere that is on the classpath 
public class ArrayItem{ 
    public int id; 
    public double att1; 
    public boolean att2; 
} 

public class Container{ 
    public List<ArrayItem> myArray; 
} 

// In your code 
Gson gson = new Gson(); 

Container container = gson.fromJson(json, Container.class); 

for(ArrayItem item : container.myArray){ 
    System.out.println(item.id); // 1, 2, 3 
    System.out.println(item.att1); // 14.2, 13.2, 13.0 
    System.out.println(item.att2); // false, false, false 
} 

Allo stesso modo, si può andare a ritroso anche molto facilmente.

String jsonString = gson.toJson(container); 
// jsonString no contains something like this: 
// {"myArray":[{"id":1,"att1":14.2,"att2":false},{"id":2,"att1":13.2,"att2":false},{"id":3,"att1":13.0,"att2":false}]} 

Il vantaggio principale che usando qualcosa come GSON fornisce è che ora è possibile utilizzare tutti i controllo dei tipi di Java per default, invece di dover gestire nomi degli attributi e tipi di te. Permette anche di fare alcune cose fantasiose come la replica delle gerarchie di tipi che rendono la gestione di un gran numero di messaggi JSON in un attimo. Funziona alla grande con Android, e il jar stesso è piccolo e non richiede alcuna dipendenza aggiuntiva.