gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 12:28:17 24 4
gpt4 key购买 nike

我正在尝试使用 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 ]
]
]
}
}
}

提前致谢

最佳答案

作为 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
))
))
))
);

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

关于C# Newtonsoft JArray。 JSON 无法在空数组中创建空数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43760652/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com