2016-08-03 8 views
1

Ich habe eine JSON-Antwort, die ich versuche, Werte von erfolglos zu lesen. Im Folgenden finden Sie die AntwortLibgdx lesen JSON Array Werte

{ 
    "ID": 89, 
    "Ans": ["24", "12", "1", "18" ], 
    "Que": "How Many hours are in a day?" 
} 

I-Wert ID und Que erfolgreich mit dem Code unten, aber nicht Ans Array lesen kann. Wie kann ich auch auf einzelne Werte innerhalb des Ans-Arrays zugreifen? Danke

JsonValue json = new JsonReader().parse(response); 
String _id = json.getString("ID"); 
String que = json.getString("Que"); 

//not working 
//String ans = json.getString("Ans"); 
//Json json1 = new Json(); 
//ArrayList<JsonValue> list = json1.fromJson(app.loadingScreen,ans); 

Antwort

0

Können Sie nicht auf diese Weise erreichen?

ArrayList<String> ansList=new ArrayList(); 

JSONArray array= mainObject.optJSONArray("Ans"); 

for(int i=0; i <array.length();i++) 
{ 

ansList.add(array.get(i)); 

} 
+0

Verwendung Es gibt keine JSONArray in Libgdx leider ... github.com/libgdx/libgdx/wiki/Reading-&-writing -JSON – Bmbariah

+0

bitte http://stackoverflow.com/questions/28266823/java-libgdx-how-to-parse-an-json –

0

sollte dies tun:

JsonValue json = new JsonReader().parse(file); 
JsonValue child = json.child; 
int id = child.asInt()); 
child = child.next; 
int[] ans = child.asIntArray(); 
child = child.next; 
String que = child.asString(); 

obwohl nicht elegant, auch JsonIterator versuchen.

UPD:

JsonValue json = new JsonReader().parse(file); 
JsonValue.JsonIterator it = json.iterator(); 
int id = it.next().asInt(); 
int[] ans = it.next().asIntArray(); 
String que = it.next().asString(); 
0

ich es geschafft, durch

String[] answers = ans.asStringArray(); 
    Gdx.app.log("App", "? : " + answers[0]);