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

1
  • See this answer. Commented Jun 11, 2012 at 23:07

1 Answer 1

1

What if you start by invoking new JSONObject(result); and then pulling an array out of that? I suspect you're trying to pull an array out of something that is not an array. Your PHP should not return something wrapped in [ ] it should return it wrapped in { }... also I believe your third JSON element (the array itself) is just hanging about without a label, which I believe is illegal.

so... if your php produced this:

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

and your java was this:

JSONObject j = new JSONObject(result);

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

I believe you'd have what you are after.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.