0

I'm having trouble parsing a json response. Its created via php and sent back to my phone via http as json.

It is an array with 3 arrays in it so...

$arr = array();

Then I search my mysql database for specific IDs relating to the query, usually about 3 results are returned with unique ids as the array index.. like this

$sql = mysql_query("Select from so and so");
while($row = mysql_fetch_assoc($sql)){
    $arr[$row["ID"]] = $row; 
}
print(json_encode($arr));

so now in my android (java) code I'm trying to convert the response to a json object and then parse it with

json_object.getString("FirstName")

for all 3 of the first names returned but its crashing. So i am guess I need to parse out the 3 individual arrays first which is where I am stuck.

-The question is how do I sort out the arrays returned within this one object. Each of them have the same keys, but different values

-crashing wasnt a good choice in terms, what I should have said is it cant find the value I am searching for when I use the getString method, here is the return

4
  • There's no question here. Or JSON. Commented Dec 10, 2011 at 1:18
  • Agreed. You need to post a print_r($arr) and probably also the results of json_encode($arr) ... and ask a question Commented Dec 10, 2011 at 1:21
  • The question is how do I sort out the arrays returned within this one object. Each of them have the same keys, but different values Commented Dec 10, 2011 at 1:24
  • Define "but its crashing" and post the actual JSON. We really can't read minds. Commented Dec 10, 2011 at 1:41

1 Answer 1

1

Agree with the comments above, would need more details.

But if you're getting back a properly formatted JSON response and it's an Array, you could always do something like ...

JSONArray results = new JSONArray(<json response string>);

for (int i=0; i<results.length(); i++) {
     JSONObject obj = results.getJSONObject(i);
}

And documentation for your reference:

http://developer.android.com/reference/org/json/JSONArray.html

1
  • Thank you... this is exactly what I was looking for, if I was popular I'd +1 you. Commented Dec 10, 2011 at 2:03

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.