簡體   English   中英

如何在1個字符串中分離2個帶條件的JSONObject? 在Java(Android)中

[英]How to seperate 2 pieces of concatinated JSONObject in 1 string? in Java (Android)

是的,作為問題標題。 我在android中收到了一個包含2個JSONObject的字符串。
我必須在PHP中執行2個不同的過程,但是兩個結果都以單字符串結果返回(回顯),我不知道如何將其分開。
我正在使用 :

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


結果字符串示例:

{"username":"xx","activated":"0"}{"multicast_id":xxx,"success":0,"failure":0,"canonical_ids":0,"results":[{"message_id":"xxx"}]}


我能怎么做?
(順便說一句第二個字符串是Firebase Cloud Messinging結果示例。
編輯 :我正在使用PHP發送FCM,這就是為什么即使我沒有(PHP :: echo)結果也被迫與其他字符串結果一起返回的原因)

String.split()與適當的regex

這是工作代碼:

    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 + "\nString2: " + str2);

OUTPUT:

D/STRING: String1: {"username":"xx","activated":"0"}
          String2: {"multicast_id":xxx,"success":0,"failure":0,"canonical_ids":0,"results":[{"message_id":"xxx"}]}

希望這會有所幫助〜

String.split()與適當的regex

使用此網站揭示正則表達式的奧秘

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM