简体   繁体   English

处理多个JSON对象(Android Java)

[英]Dealing with multiple JSON objects (Android Java)

I am currently dealing with some JSON and apparently running into a bit of trouble. 我目前正在处理一些JSON,显然遇到了一些麻烦。 I have a PHP page that fetches two unrelated MySQL requests at the same time and displays both of them, one after the other. 我有一个PHP页面,可同时获取两个不相关的MySQL请求,并一个接一个地显示它们。 I have two JSON encodings. 我有两种JSON编码。 My issue is, I can't get my Java program to recognize the second one. 我的问题是,我的Java程序无法识别第二个程序。 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. 我通过在线验证器运行了JSON,很明显,这两个不应该像现在这样。 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. 请注意,它们之间的逗号(第11行)是手动添加的,因为我认为这会有所帮助。 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. 我可以很好地解析player_update,但是在我要求找到player_list之后,什么也没做,我的日志就停在那里。 Test 00 AND Test 1 both don't display. 测试00和测试1均不显示。

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); 我的PHP页面包括:json_encode($ array1); echo“,” ;; json_encode($ array2); But the comma is useless. 但是逗号是没有用的。 Maybe if my JSON was valid then it would work better. 也许如果我的JSON有效,那么它会更好。

Logcat : 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

Problem: 问题:

The way you are encoding JSON is wrong. 您编码JSON的方式是错误的。 That was not an example of valid JSON formatted data. 那不是有效的JSON格式数据的示例。 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. 然后将它们都合并为单个数组,然后以json格式编码。

Example: (in your php file) 示例:(在您的php文件中)

$data_to_send=array(); $ data_to_send = array();
$data_to_send['player_update']=$player_update; $ data_to_send ['player_update'] = $ player_update; // an array of player update //一组玩家更新
$data_to_send['player_list']=$player_list; $ data_to_send ['player_list'] = $ player_list; // an array of player_list //一个player_list数组

json_encode($data_to_send); json_encode($ data_to_send); // to send the response //发送响应

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

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