我在 JObject 中添加了一个新孩子。可行。
var fi = e.PropertyA;
JObject o = JObject.FromObject(fi);
o.AddFirst(new JProperty("type", new JValue(fi.GetType().Name)));
// this works
o.Add("description", new JObject( new JProperty("de", new JValue("Wert"))));
var node = o.SelectToken("description");
if (node != null)
{
// this fails
// node.AddAfterSelf(new JProperty("en", "Value"));
}
"PropertyA": {
"type": "SubObject",
"Foo": "foo",
"description": {
"de": "Wert"
}
}
之后,我尝试将一个新对象添加到之前创建的令牌中。失败并显示以下错误消息:
Newtonsoft.Json.JsonException:Newtonsoft.Json.Linq.JProperty不能有多个值
要重现该错误行为,请删除此fiddle中的注释。
我需要的是这样的国际化JSON对象。
一个客户希望获得英语信息
{
"venue_id": "C001",
"venue_location": "Baker St."
"venue_name": "Aroma Coffee"
}
另一个客户希望以希伯来语获取信息
{
"venue_id": "C001",
"venue_location": "רחוב האופה"
"venue_name": "קפה ארומה"
}
结果应该像这样
{
"venue_id": "C001",
"venue_location": {
"en": "Baker St.",
"he": "רחוב האופה"
}
"venue_name": {
"en": "Aroma Coffee",
"he": "קפה ארומה"
}
}
答案 0 :(得分:0)
因此有效
o.Add("description", new JObject( new JProperty("de", new JValue("Wert"))));
var node = o.SelectToken("description") as JObject;
if (node != null)
{
node.Add(new JProperty("en", "Value"));
}
产生
"PropertyA": {
"type": "SubObject",
"Foo": "foo",
"description": {
"de": "Wert",
"en": "Value"
}
}
@ er-shoaib:感谢您的努力