我喜欢这样的字典-
Dictionary<string, Object> dict = new Dictionary<string, Object>();
dict.Add("event.data", "data");
dict.Add("event.Location", LocObj);
dict.Add("event.Details.Cost", CostObj);
我正在尝试将其与现有的json结构合并-
{
"event" : {
"name" : "Hello World",
"tags" : [tags]
}
"sponsors" : {
...
}
"OtherObj" : {
...
}
}
我正在尝试使用ExpandoObject,然后将其注入到原始json中,就像这样-
foreach(KeyValuePair<string, Object> kvp in dict) {
var newObj= new ExpandoObject() as IDictionary<string, Object>;
newObj.Add(kvp.Key, value);
existingObject.Merge(JObject.FromObject(newObj), new JsonMergeSettings
{
MergeArrayHandling = MergeArrayHandling.Union
});
}
Console.WriteLine(JsonConvert.SerializeObject(existingObject));
但是当它被序列化时,[[event.Location不会显示在现有事件标记中,而是添加了一个名为event.Location的新键。
{
"event" : {
"name" : "Hello World",
"tags" : [tags]
},
"event.Location" : { ... }, <---------------
"event.data": { ... }, <---------------
"event.Details.Cost" : { ... }<---------------
"sponsors" : {
...
},
"OtherObj" : {
...
}
}
如何修复Expando obj创建来解决此问题?