Yes, as question title. I got a single string received in android consist of 2 JSONObject.
I have to do 2 different processes in PHP but both result are returned (echo) in single string result which I don't know how to seperate it.
I'm using :
JSONObject json = new JSONObject(result);
String success = json.optString("success");
// "success" here shows empty in logcat. I think it doesn't get the second json object
Example of Result string:
{"username":"xx","activated":"0"}{"multicast_id":xxx,"success":0,"failure":0,"canonical_ids":0,"results":[{"message_id":"xxx"}]}
How can I do?
(btw 2nd string is Firebase Cloud Messanging result example.
EDIT: I'm using PHP to send FCM, that's why the result is forced to return together with my other string result even I do not (PHP::echo) it)
Use String.split()
with proper regex
.
Here is the working code:
String response = "{\"username\":\"xx\",\"activated\":\"0\"}{\"multicast_id\":xxx,\"success\":0,\"failure\":0,\"canonical_ids\":0,\"results\":[{\"message_id\":\"xxx\"}]}";
String[] separated = response.split("\\}\\{");
String str1 = separated[0] + "}";
String str2 = "{" + separated[1];
Log.d("STRING", "String1: " + str1 + "
String2: " + str2);
OUTPUT:
D/STRING: String1: {"username":"xx","activated":"0"}
String2: {"multicast_id":xxx,"success":0,"failure":0,"canonical_ids":0,"results":[{"message_id":"xxx"}]}
Hope this will help~