无法解析android中的嵌套json块for FCM

无法解析android中的嵌套json块for FCM

问题描述:

我正在使用FCM推送消息并处理onMessageReceived中的所有传入推送通知。现在的问题是解析这个函数内嵌入的json remoteMessage.getData()无法解析android中的嵌套json块for FCM

我有下面的块作为设备中的推送通知。数据有效载荷的内容可以在这里改变它的经销商后,它可能会productInfo

{ 
    "to": "/topics/DATA", 
    "priority": "high", 
    "data": { 
    "type": 6, 
    "dealerInfo": { 
     "dealerId": "358", 
     "operationCode": 2 
    } 
    } 
} 

这个我怎么分析它

if(remoteMessage.getData()!=null){ 

     JSONObject object = null; 
     try { 
      object = new JSONObject(remoteMessage.getData());  

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 


    } 

现在我为remoteMessage.getData()回报Map<String,String>所以大概用blackslashes获取数据我的嵌套块被转换成字符串不知道。

{ 
    "wasTapped": false, 
    "dealerInfo": "{\"dealerId\":\"358\",\"operationCode\":2}", 
    "type": "6" 
} 

,如果我写object = new JSONObject(remoteMessage.getData().toString());那么它得到了失败,以下通知

{ 
    "to": "regid", 
    "priority": "high", 
    "notification" : { 
     "body": "Message Body", 
     "title" : "Call Status", 
     "click_action":"FCM_PLUGIN_ACTIVITY" 
    }, 
    "data": { 
    "type": 1, 
    "callNumber":"ICI17012702", 
    "callTempId":"0", 
     "body": "Message Body", 
     "title" : "Call Status" 
    } 
} 

错误我得到的是

> org.json.JSONException: Unterminated object at character 15 of 
> {body=Message Body, type=1, title=Call Status, callNumber=ICI17012702, 
> callTempId=0} 

试试这个代码:

public void onMessageReceived(RemoteMessage remoteMessage) 
    { 
     Log.e("DATA",remoteMessage.getData().toString()); 
     try 
     { 
      Map<String, String> params = remoteMessage.getData(); 
      JSONObject object = new JSONObject(params); 
      Log.e("JSON OBJECT", object.toString()); 
      String callNumber = object.getString("callNumber"); 
      //rest of the code 
     } 
    } 

还要确保您的JSON有效使用This

+0

不可能这样如何将我访问dealerInfo部分 – Hunt

+0

嵌套对象仍停留在这种情况下,一个字符串。 –

+0

@Abhinav这是什么意思?答案的作品...给downvote ...的正当理由!!答案可能不适用于你...所以你可以添加你自己的版本,而不是抓别人的背部...... – rafsanahmad007

从GCM迁移到FCM时遇到此问题。

以下是我的使用案例(和OP有效载荷),所以也许它会适用于其他人。

JsonObject jsonObject = new JsonObject(); // com.google.gson.JsonObject 
JsonParser jsonParser = new JsonParser(); // com.google.gson.JsonParser 
Map<String, String> map = remoteMessage.getData(); 
String val; 

for (String key : map.keySet()) { 
    val = map.get(key); 
    try { 
     jsonObject.add(key, jsonParser.parse(val)); 
    } catch (Exception e) { 
     jsonObject.addProperty(key, val); 
    } 
} 

// Now you can traverse jsonObject, or use to populate a custom object: 
// MyObj o = new Gson().fromJson(jsonObject, MyObj.class) 

我已经改变到

的JSONObject JSON =新的JSONObject(remoteMessage.getData());

的JSONObject JSON =新的JSONObject(remoteMessage.getData()的toString());

工作正常。

由于dealerInfo被解析为字符串,而不是一个对象,创建一个新的JSONObject与字符串

JSONObject dealerInfo = new JSONObject(object.getString("dealerInfo")); 
String dealerId = dealerInfo.getString("dealerId"); 
String operationCode = dealerInfo.getString("operationCode");