0

我想以以下格式从 c# 发送 Json 字符串以用于 IOS 推送通知:

{
    "pushType": "Notification",
    "notifications": [
        {
            "notificationId": 1,
            "notificationTitle": "Notification 1",
            "notificationText": "You are reading your notification 1",
            "notificationExpiryDate": "yyyy-MM-dd"
        },
        {
            "notificationId": 2,
            "notificationTitle": "Notification 2",
            "notificationText": "You are reading your notification 2",
            "notificationExpiryDate": "yyyy-MM-dd"
        },
        {
            "notificationId": 3,
            "notificationTitle": "Notification 3",
            "notificationText": "You are reading your notification 3",
            "notificationExpiryDate": "yyyy-MM-dd"
        }
    ]
}

我试图发送简单的消息,但无法发送字符串数组。请帮我解决这个问题,因为我在过去两天一直在苦苦挣扎。

4

1 回答 1

0

您可以通过使用 JavascriptSerializer 来实现这一点。这是使用匿名代码声明的代码示例

var obj = new
        {
            pushType = "Notification",
            notification = new[]{
                new {
                notificationId = 1,
                notificationTitle = "Notification 1",
                notificationText = "You are reading your notification 1",
                notificationExpiryDate = "yyyy-MM-dd"
                },
                new {
                notificationId = 2,
                notificationTitle = "Notification 2",
                notificationText = "You are reading your notification 2",
                notificationExpiryDate = "yyyy-MM-dd"
                },
                new {
                notificationId = 3,
                notificationTitle = "Notification 3",
                notificationText = "You are reading your notification 3",
                notificationExpiryDate = "yyyy-MM-dd"
                }
            }
        };            

        JavaScriptSerializer serializer = new JavaScriptSerializer();
        string outputstring = serializer.Serialize(obj);

希望能帮助到你。

于 2013-06-18T14:37:17.720 回答