将分隔字符串转换为 Expando 对象

Convert delimited string to Expando Object

我有这样一本字典-

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 并没有出现在现有的事件标签中,而是添加了一个名为 [=28 的新键=].

{
   "event" : {
       "name" : "Hello World",
       "tags" : [tags]
    },
    "event.Location" : { ... },   <---------------
    "event.data": { ... },        <---------------        
    "event.Details.Cost" : { ... }<---------------        
    "sponsors" : {
       ...
    },
    "OtherObj" : {
       ...
    }
 }

如何修复 Expando obj 创建来修复此问题?

看下面的程序。这将显示一个工作合并和中断合并的示例。不同之处在于对象的结构。在一种情况下,"Event.Location" 不起作用。在另一种情况下,"Event" 对象包含一个 "Location" 并且它正确地嵌套在 JObject 的合并操作中。

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Dynamic;

namespace DynamicSerial
{
    class Program
    {
        static void Main(string[] args)
        {
            var myObject = new Dictionary<string, Object>();
            myObject.Add("Id", 1);
            myObject.Add("Event", new { Name = "SomeName", Id = 2 });

            var mergeObject = new Dictionary<string, Object>();
            mergeObject.Add("Event", new { Location = new { Name = "SomeLocation", Id = 3 } }); //WORKS
            //mergeObject.Add("Event.Location", new { Name = "SomeLocation", Id = 3 }); //DOES NOT WORK

            JObject myDynamicObject = JObject.FromObject(myObject);

            foreach (KeyValuePair<string, Object> kvp in mergeObject)
            {

                var newObj = new ExpandoObject() as IDictionary<string, Object>;
                newObj.Add(kvp.Key, kvp.Value);

                myDynamicObject.Merge(JObject.FromObject(newObj), new JsonMergeSettings
                {
                    MergeArrayHandling = MergeArrayHandling.Merge
                });
            }

            Console.WriteLine(JsonConvert.SerializeObject(myDynamicObject));

        }
    }
}

结果JSON

good:
{"Id":1,"Event":{"Name":"SomeName","Id":2,"Location":{"Name":"SomeLocation","Id":3}}}
bad:
{"Id":1,"Event":{"Name":"SomeName","Id":2},"Event.Location":{"Name":"SomeLocation","Id":3}}

总之,你应该改变

Dictionary<string, Object> dict = new Dictionary<string, Object>();
dict.Add("event.data", "data");
dict.Add("event.Location", LocObj);
dict.Add("event.Details.Cost", CostObj);

Dictionary<string, Object> dict = new Dictionary<string, Object>();
dict.Add("event", new { data="data", Location=LocObj, Details=new { Cost=CostObj } });