如何使用.net c#构建这个json



我想建立一个json:

...
  "conditions": [
      [
        "Language",
        "IN",
        [
          "en"
        ]
      ]
    ]
...

我正在使用这个代码:

new JProperty("conditions", new JArray(new JArray((new JArray("Language", "IN", new JArray(IsEnglish ? "en" : "es"))))))

但是这个创建了一个json,少了一个数组。

  "conditions": 
      [
        "Language",
        "IN",
        [
          "en"
        ]
      ]

我试图将此嵌套到另一个JArray中,但这不起作用。请建议。

编辑:这是整个Json制作语句

 JObject 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("content", new JObject(new JProperty("en", pushContentEnglish), new JProperty("es", pushContentSpanish))),
                       new JProperty("ios_badges", 0),
                       new JProperty("data", new JObject(new JProperty("custom", new JObject(new JProperty("t", notificationType), new JProperty("i", objectId))))),
                       new JProperty("devices", new JArray(strDeviceArray)),
                       new JProperty("conditions", new JArray(new JArray((new JArray("Language", "IN", new JArray(IsEnglish ? "en" : "es")))))
                       )))));

这似乎可以工作:

new JProperty("conditions", new JArray((JContainer)new JArray("Language", "IN", new JArray(IsEnglish ? "en" : "es"))))

这样做的原因是JArray(JArray)构造函数将元素从JArray参数复制到正在构造的JArray中,而将其转换为JContainer (JArray继承的对象)会调用JArray(Object)重载。

如果您使用Json。Net,为什么不这样做:

var conditions = new[] { new object[] { "Language", "IN", new[] { "en" } } };
var result = "conditions:" + JsonConvert.SerializeObject(conditions);

我认为你只使用一个数据…

"conditions": [
  [
    "Language",
    "IN",
    [
      "en"
    ]
  ]
]

但是如果你想再做一个…

"conditions": [
    [
        "Language",
        "IN",
        [
            "en"
        ]
    ],
    [
        "Language",
        "IN",
        [
            "pt"
        ]
    ]
]

如果你可以用这个预期数组中的多个数据来测试这段代码…? 你会试试这个吗?

相关内容

  • 没有找到相关文章

最新更新