处理多个JSON对象(Android Java)

I am currently dealing with some JSON and apparently running into a bit of trouble. I have a PHP page that fetches two unrelated MySQL requests at the same time and displays both of them, one after the other. I have two JSON encodings. My issue is, I can't get my Java program to recognize the second one. First one is parsed fine.

I ran the JSON through an online validator and it is quite clear those two shouldn't follow as they are now. What is the correct way of dealing with those two ?

Please note that the comma between them (line 11) was added manually because I thought it would help. It didn't.

{  
   "player_update":[  
      {  
         "id":"16",
         "name":"Phil_TEST",
         "last_login":"2015-10-12 00:36:05",
         "for_update":"00:00:00",
         "newplayer":"no"
      }
   ]
},
{  
   "player_list":[  
      {  
         "id":"16",
         "name":"Phil_TEST",
         "last_login":"2015-10-12 01:00:42"
      },
      {  
         "id":"15",
         "name":"Phil8",
         "last_login":"2015-10-12 00:50:49"
      }
   ]
}

Edit : here's the code I'm using. I can parse the player_update fine, but nothing is done after I ask to find the the player_list, my Logs stop there. Test 00 AND Test 1 both don't display.

JSONObject obj = new JSONObject(stream);

JSONArray arr_player_update = obj.getJSONArray("player_update");
String newplayer = arr_player_update.getJSONObject(0).getString("newplayer");
Log.i("PhLog LobbyActivity", "Newplayer : "+newplayer);

Log.i("PhLog LobbyActivity", "Test 0");
JSONArray arr_player_list = obj.getJSONArray("player_list");
Log.i("PhLog LobbyActivity", "Test 00");
for (int i = 0; i < arr_player_list.length(); i++) {
    String id = arr_player_list.getJSONObject(i).getString("id");
    String name = arr_player_list.getJSONObject(i).getString("name");
    String last_login = arr_player_list.getJSONObject(i).getString("last_login");
}

My PHP pages consists of : json_encode($array1);echo",";json_encode($array2); But the comma is useless. Maybe if my JSON was valid then it would work better.

Logcat :

10-12 09:48:00.086 1052-1052/? I/PhLog LobbyActivity: Newplayer : no
10-12 09:48:00.086 1052-1052/? I/PhLog LobbyActivity: Test 0
10-12 09:48:00.086 1052-1052/? W/System.err: org.json.JSONException: No value for player_list
duangouyan3328
duangouyan3328 您应该将两个数组附加到单个数组中,然后运行json_encode($array);echo;
大约 5 年之前 回复
donlih2986
donlih2986 使用我的JSON解析代码编辑。我认为问题来自于要解析的JSON,但我不确定如何使其有效。
大约 5 年之前 回复
dre75230
dre75230 你能发布你的代码进行解析吗?
大约 5 年之前 回复
dqunzip3183
dqunzip3183 您使用什么例程来解析JSON?最佳解决方案可能是对您的服务器进行两次调用,或者将其转换为有效的JSON。如果您使用的是gson,则可以使用一种技巧来解析同一个流中的多个json块
大约 5 年之前 回复
duankuangxie9070
duankuangxie9070 一旦你完成了第一个JSON,除了第二个JSON之外,全部冲洗掉。然后再次通过网格发送它。这可能有用。也许这是你的缓存
大约 5 年之前 回复

1个回答



问题:</ p>

编码JSON的方式是错误的。 这不是有效JSON格式数据的示例。 您可以在此处查看其有效性。</ p>

解决方案:</ p>

您有两个不同的数组作为响应发送。 然后首先将它们组合在单个数组中,然后以json格式对其进行编码。</ p>

示例:(在您的php文件中)</ p>

$ data_to_send = 阵列();结果,
$的data_to_send [ 'player_update'] = $ player_update; //一个玩家更新数组

$ data_to_send ['player_list'] = $ player_list; //一个player_list数组</ p>

json_encode($ data_to_send); //发送回复</ p>
</ div>

展开原文

原文

Problem:

The way you are encoding JSON is wrong. That was not an example of valid JSON formatted data. You can check it's validness here.

Solution :

You have two different arrays to send as response. Then combine both of them in single array first and then encode it in json format.

Example: (in your php file)

$data_to_send=array();
$data_to_send['player_update']=$player_update; // an array of player update
$data_to_send['player_list']=$player_list; // an array of player_list

json_encode($data_to_send); // to send the response

douxian1939
douxian1939 工作完美无缺,我甚至不需要更改任何其他代码。 谢谢 ! 所以你不能同时处理多个编码,你只需要做一个包含其他代码的代码。
大约 5 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐