I have a Jobject
Car
.
The Car
has a null
collection 'wheels'.
How could I add property and values to the collection wheels
?
Code:
{
"Car": {
"engine": 2,
"wheels": []
}
}
What I expect:
{
"Car": {
"engine": 2,
"wheels": [
{
"frontWheel": 2,
"rearWheel": 2
}
]
}
}
How to add frontwheel
and rearwheel
to the JObject
?
To create your desired output using Newtonsoft.Json
, you create an instance of JArray
and populate it with instances of type JObject
.
var result = new JObject(
new JProperty("engine", 2),
new JProperty("wheels",
new JArray
{
new JObject(
new JProperty("frontWheel", 2),
new JProperty("rearWheel", 2))
}));
This will produce the following output:
{
"engine": 2,
"wheels": [
{
"frontWheel": 2,
"rearWheel": 2
}
]
}
Alternatively you could use the serialization mechanism provided by Newtonsoft.Json
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.