简体   繁体   English

从php-> java中解析具有多个数组的json数组

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

I'm having trouble parsing a json response. 我在解析json响应时遇到问题。 Its created via php and sent back to my phone via http as json. 它是通过php创建的,并通过json作为http发送回我的手机。

It is an array with 3 arrays in it so... 它是一个包含3个数组的数组,因此...

$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 然后我在mysql数据库中搜索与查询相关的特定ID,通常返回大约3个结果,并以唯一ID作为数组索引。

$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 所以现在在我的android(java)代码中,我试图将响应转换为json对象,然后使用

json_object.getString("FirstName")

for all 3 of the first names returned but its crashing. 返回的所有3个名字,但崩溃了。 So i am guess I need to parse out the 3 individual arrays first which is where I am stuck. 所以我想我需要首先解析3个单独的数组,这就是我遇到的问题。

-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 -crashing并不是一个好的选择,我应该说的是,当我使用getString方法时,它找不到我要搜索的值,这是返回值

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 ... 但是,如果您要获取格式正确的JSON响应,并且它是一个数组,则可以始终执行类似...

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 http://developer.android.com/reference/org/json/JSONArray.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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