【发布时间】:2019-10-04 15:30:40
【问题描述】:
我的Jobject如下:
JObject sampleObj = "{
"operator": "AND",
"rules": [
{
"field": "Entity.Country",
"condition": "=",
"value": "'USA'"
},
{
"field": "Entity.ShortName",
"condition": "=",
"value": "'Adele'"
}
]
}"
如何在 JSON 的 JArray“规则”中添加另一个 JObject?如下所示
JObject Parent =
"{
"operator": "AND",
"rules": [{
"field": "Entity.Country",
"condition": "=",
"value": "'USA'"
},
{
"field": "Entity.ShortName",
"condition": "=",
"value": "'Adele'"
},
{
"operator": "AND",
"rules": [{
"field": "Entity.Country",
"condition": "=",
"value": "'USA'"
},
{
"field": "Entity.ShortName",
"condition": "=",
"value": "'Adele'"
}
]
}
]
}"
我试过了:
Parent["rules"].Add(sampleObj);
Parent.selectToken("rules").Add(sampleObj);
但是,智能感知不允许我这样做。工作 C# + Newtonsoft.Json 库:
'JToken' does not contain a definition for 'Add' and no accessible extension method 'Add' accepting a first argument of type 'JToken' could be found (are you missing a using directive or an assembly reference?)
【问题讨论】:
-
听起来你需要投射。
((JArray)Parent["rules"]).Add(sampleObj)