根据 c# 类中的条件变量设置 json 对象的格式



我在WCF服务中使用PushWoosh来向iOS设备或Android发送推送通知。使用以下 c# 类:

[DataContract]
public class PendingBadge
{
    [DataMember]
    public int BadgeCount { get; set; }
    [DataMember]
    public List<Uuid> Uuids { get; set; }
}
[DataContract]
public class Uuid
{
    [DataMember]
    public string UuidId { get; set; }
    [DataMember]
    public string DevicePlatform { get; set; }
}

其中UuidId =唯一的设备ID,DevicePlatform ='android'或'iOS',我正在创建一个json对象发送到PushWoosh。

var totalBadgeCount = new PendingBadge();
//code instantiating Uuid class and creating a list of Uuid from a database
//and adding said list to totalBadgeCount
string pwAuth ="XXXXXXXXXXXXXXXXXXX";
string pwApplication = "XXXXXXXXX";
var json = new JObject(
                 new JProperty("application", pwApplication),
                 new JProperty("auth", pwAuth),
                 new JProperty("notifications",
                       new JArray(
                             new JObject(
                                   new JProperty("send_date", "now"),
                                   new JProperty("ignore_user_timezone", true),
                                   new JProperty("platforms", 1),
                                   new JProperty("ios_badges", totalBadgeCount.BadgeCount),
                                   new JProperty("devices", uuid.UuidId)
                       ))));
 PwCall("createMessage", json);

我需要做的是在 json 对象上编写某种条件语句,如下所示:

  • 如果 DevicePlatform 是"android",请将"平台"JProperty 设置为等于 3 或等于 1 对于"iOS"。
  • 根据平台添加 JProperty "android_header"或 JProperty "ios_badges"。
  • 根据平台连接每个 uuidId 的逗号分隔字符串。

我在概念化如何做到这一点时遇到了一点麻烦。如果有人能帮助我解决这个问题,我将不胜感激。

我误读了这个问题。这是我相信您正在问的半完整解决方案。

编辑:解决方案的代码示例

string pwAuth = "XXXXXXXXXXXXXXXXXXX";
string pwApplication = "XXXXXXXXX";
var devicePlatform = "android";
var json = new JObject(
                    new JProperty("application", pwApplication),
                    new JProperty("auth", pwAuth),
                    new JProperty("notifications",
                        new JArray(
                                new JObject(
                                    new JProperty("send_date", "now"),
                                    new JProperty("ignore_user_timezone", true),
                                    new JProperty("platforms", devicePlatform == "android" ? 3 : 1),
                                    new JProperty(devicePlatform == "android" ? "android_header" : "ios_badges", totalBadgeCount.BadgeCount),
                                    new JProperty("devices", String.Join(",", totalBadgeCount.Uuids.Select(u => u.UuidId)))
                        ))));

下面是如何执行此操作的示例:

string pwApplication = "XXXXXXXXX";
bool isAndroid = uuid.DevicePlatform == "android";
var myNotif = new JObject(
                new JProperty("send_date", "now"),
                new JProperty("ignore_user_timezone", true),
                new JProperty("platforms", isAndroid ? 3 : 1),
                new JProperty("devices", uuid.UuidId)
                );
if (isAndroid)
{
    myNotif.Add(new JProperty("android_header", "whatever you put here"));
}
else
{
    myNotif.Add(new JProperty("ios_badges", totalBadgeCount.BadgeCount));
}
myNotif.Add(new JProperty("uuid_list", string.Join(",",
   totalBadgeCount.Uuids.Where(x => x.DevicePlatform == uuid.DevicePlatform)
                        .Select(x => x.UuidId))));
var json = new JObject(
    new JProperty("application", pwApplication),
    new JProperty("auth", pwAuth),
    new JProperty("notifications",
        new JArray(myNotif)));
PwCall("createMessage", json);

我认为您的代码可以通过使用类而不是直接JObject s(等)来改进。 例如

public class Notification
{
    public string send_date { get; set; }
    public bool ignore_user_timezone { get; set; }
    public int platforms { get; set; }
    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
    public int? ios_badges { get; set; }
    public string devices { get; set; }
    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
    public string android_header { get; set; }
    public string uuid_list { get; set; }
}
public class MyMessage
{
    public string application { get; set; }
    public string auth { get; set; }
    public List<Notification> notifications { get; set; }
}
string pwApplication = "XXXXXXXXX";
bool isAndroid = uuid.DevicePlatform == "android";
var myNotif =
    new Notification
    {
        send_date = "now",
        ignore_user_timezone = true,
        platforms = isAndroid ? 3 : 1,
        devices = uuid.UuidId
    };
if (isAndroid)
{
    myNotif.android_header = "whatever you put here";
}
else
{
    myNotif.ios_badges = totalBadgeCount.BadgeCount;
}
myNotif.uuid_list = string.Join(",",
   totalBadgeCount.Uuids.Where(x => x.DevicePlatform == uuid.DevicePlatform)
                        .Select(x => x.UuidId));
var myMessage = new MyMessage
{
    application = pwApplication,
    auth = pwAuth,
    notifications = new List<Notification> { myNotif }
};
var json = JsonConvert.SerializeObject(myMessage);
PwCall("createMessage", json);

相关内容

  • 没有找到相关文章

最新更新