[英]How to add a json array into a property of a JObject with json.net
[英]How to add json property and value to an empty collection in a jobject?
我有一輛Jobject
Car
。
Car
有一個null
'wheels'。
我如何向收集wheels
添加屬性和值?
代碼:
{
"Car": {
"engine": 2,
"wheels": []
}
}
我的期望:
{
"Car": {
"engine": 2,
"wheels": [
{
"frontWheel": 2,
"rearWheel": 2
}
]
}
}
如何將frontwheel
和rearwheel
添加到JObject
?
要使用Newtonsoft.Json
創建所需的輸出,請創建JArray
的實例並使用JObject
類型的實例填充它。
var result = new JObject(
new JProperty("engine", 2),
new JProperty("wheels",
new JArray
{
new JObject(
new JProperty("frontWheel", 2),
new JProperty("rearWheel", 2))
}));
這將產生以下輸出:
{
"engine": 2,
"wheels": [
{
"frontWheel": 2,
"rearWheel": 2
}
]
}
或者,您可以使用Newtonsoft.Json
提供的序列化機制
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.