C#Newtonsoft JArray。 JSON无法在空数组中创建空数组

时间:2017-05-03 12:54:12

标签: c# json

我试图用Newtonsoft创建一个JSON对象。一切看起来都不错但我无法在空数组中创建空数组。我需要以下输出......

我的代码:

JObject rss = new JObject(
  new JProperty("query",
    new JObject(
      new JProperty("aoi",
        new JObject(
          new JProperty("type", "Polygon"),
            new JProperty("coordinates",
              new JArray(
                new JArray(
                  new JArray(
                    new JValue(-122.62664794921874),
                      new JValue(38.81403111409755)
                    ),
                    new JArray(
                      new JValue(-122.62664794921874),
                      new JValue(38.81403111409755)
                    )
                  )
                )
              )
            )
          )
        )
      )
    );

我得到了什么:

{
  "query": {
    "aoi": {
      "type": "Polygon",
      "coordinates": [
        [ -122.62664794921874, 38.81403111409755 ],
        [ -122.62664794921874, 39.07464374293249 ]
      ]
    }
  }
}

我需要什么:

{
  "query": {
    "aoi": {
      "type": "Polygon",
      "coordinates": [
        [
          [ -122.62664794921874, 38.81403111409755 ],
          [ -122.62664794921874, 39.07464374293249 ]
        ]
      ]
    }
  }
}

提前致谢

1 个答案:

答案 0 :(得分:1)

作为JArray构造函数的参数的单个JArray被解释为应该复制到新JArray的内容。如果您这样做,它可以工作:

 JObject rss = new JObject(
        new JProperty("query",
            new JObject(
                new JProperty("aoi",
                    new JObject(
                        new JProperty("type", "Polygon"),
                        new JProperty("coordinates",
                            new JArray(
                                new JArray(
                                    new JArray(
                                        new JValue(-122.62664794921874),
                                        new JValue(38.81403111409755)
                                        ),
                                    new JArray(
                                        new JValue(-122.62664794921874),
                                        new JValue(38.81403111409755)
                                        )
                                ) as Object
                        ))
                        ))
                ))
        );          

这使构造函数将其视为应该插入而不是复制的东西。