Android GCM:我们可以将数据作为完整的JSON结构获取吗?

时间:2013-02-27 15:03:44

标签: android google-cloud-messaging

我正在向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!"
}

2 个答案:

答案 0 :(得分:3)

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。

答案 1 :(得分:0)

JSONArray array = new JSONArray(jsonBodyOfTheResponse);

for (int i = 0; i < array.length(); i++) {
    JSONObject row = array.getJSONObject(i);
    .
    .
    . }