我正在向 Google GCM 服务器 https://android.googleapis.com/gcm/send
发送一条消息 (POST),以便向已注册的 android 设备发送推送通知。 POST 主体看起来像这样:
{
"registration_ids" : [android_registration_id],
"data" : {
"message" : "Hello World!",
"update" : "You've got a new update",
"info" : "You've got new information updates too!"
}
}
假设我不知道“数据”字段中所有键值对都发送给我(gcm注册的android应用程序),我想枚举并打印它们,我可以提取“数据”中的字段吗"作为 JSON 结构?
例如在上面的示例中,我需要以下内容作为 JSON 对象:
{
"message" : "Hello World!",
"update" : "You've got a new update",
"info" : "You've got new information updates too!"
}
最佳答案
Bundle data = intent.getExtras();
Iterator<String> it = data.keySet().iterator();
String key;
String value;
while(it.hasNext()) {
key = it.next();
value = data.getString(key);
}
试试这个。使用键和值可以构造初始 json。
关于安卓 GCM : Can we get data as a complete JSON structure?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15115385/