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