dopii22884 2015-10-11 23:26
浏览 139
已采纳

处理多个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
  • 写回答

1条回答

  • dtxob80644 2015-10-12 09:16
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制