简体   繁体   中英

Parsing json array with multiple arrays in it from php -> java

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM