2016-08-03 2 views
1

値を正しく読み込めないというjson応答があります。以下 私は以下のコードではなく、回答アレイで正常値IDとクエを読み取ることができる応答Libgdxがjson配列値を読み取る

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

あります。また、Ans配列内の個々の値にどうやってアクセスできますか?ありがとう

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

答えて

0

あなたはこのようにして達成できませんか?

ArrayList<String> ansList=new ArrayList(); 

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

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

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

} 
+0

を使用して管理-JSON – Bmbariah

+0

http://stackoverflow.com/questions/28266823/java-libgdx-how-to-parse-an-jsonを参照してください。 –

0

これが何をすべき:

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

ないエレガントにかかわらず、あなたはまた、JsonIteratorを試すことができます。

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

私はJSONArrayは悲しげにlibgdxではありません... github.com/libgdx/libgdx/wiki/Reading-&-writing

String[] answers = ans.asStringArray(); 
    Gdx.app.log("App", "? : " + answers[0]); 
関連する問題