-1

这是我的 JSON

   var postData = 
                     "{ \"registration_ids\": [ \"" + pushNotificationState.RegistrationId + "\" ], "+
                         "\"data\": {\""+ pushNotificationState.NotificationData.NotificationData + "\"}";

注册 ID 和通知数据是变量。我从 GCM 服务器收到 400 响应,说 JSON 格式不正确。任何人都可以选择我出错的地方吗?

干杯

4

4 回答 4

2

你有一个 open{但有两个 close }

var postData = "{ \"registration_ids\": [ \"" + pushNotificationState.RegistrationId + "\" ], " + "\"data\": \""+ pushNotificationState.NotificationData.NotificationData + "\"}";
于 2013-09-02T05:12:14.723 回答
1

使用一些Json工具,而不是手动创建字符串。否则,如果某些字符串变量包含{,等},您就会遇到问题。"

var json = JsonConvert.DeserializeObject(
            new {
                registration_ids = new[] { pushNotificationState.RegistrationId },
                data = pushNotificationState.NotificationData.NotificationData
            });
于 2013-09-02T05:58:46.507 回答
0
   var postData =      "{ \"registration_ids\": [ \"" + pushNotificationState.RegistrationId + "\" ]}, "+
                             "\"data\": {\""+ pushNotificationState.NotificationData.NotificationData + "\"}";

尝试用它替换它。

于 2013-09-02T05:14:50.637 回答
0

您可以使用以下代码创建请求对象,然后转换为 json。

public  class GCMRequest
{
    public GCMRequest()
    {
        data = new PayLoad();
    }
    public List<string> registration_ids;
    public PayLoad data;
}
public class PayLoad
{
    public string key;
}

您可以如下创建请求

GCMRequest req = new GCMRequest();
List<string> tokens = new List<string>();
// .. fill the tokens to the 'tokens' list
req.registration_ids = tokens;
req.data.key = "Hi, how are you"; // message you want to send
string json = new JavaScriptSerializer().Serialize(req);

希望这会有所帮助。

于 2013-11-21T09:30:13.620 回答