0

A PHP script is sending to an Android app some data in the following format

[{"event_id":"4","message":"test"},["person1","person2"]]

I want to exact the 2 elements from this array into other arrays so I can easily manipulate the data. I got to the point in which the above response from the server is being converted into as string. What I can't seem to be able to do is to parse the data into arrays. I'm trying something on the following lines:

receivedData = new JSONArray(result); //result is the string response from the server  
JSONArray array1= receivedData.getJSONArray(0);  
JSONArray array2= receivedData.getJSONArray(1);  
int len = array1.length();

but lenis not giving me back anything :(
What am I doing wrong and how could I change it.
Many thanks

4

1 に答える 1

1

new JSONObject(result); を呼び出すことから始めたらどうでしょうか。それから配列を引き出しますか?配列ではないものから配列を取り出そうとしていると思われます。あなたの PHP は [ ] でラップされたものを返すべきではありません。それは { } でラップされたものを返す必要があります。

だから...あなたのphpがこれを生成した場合:

 {"event_id":"4","message":"test"},"people": ["person1","person2"]}

あなたのJavaはこれでした:

JSONObject j = new JSONObject(result);

String [] people = j.getJSONArray("people");

私はあなたが求めているものを持っていると信じています。

于 2012-06-11T23:06:01.193 に答える