C# flattening json structure

后端 未结 5 594
囚心锁ツ
囚心锁ツ 2020-12-08 07:41

I have a json-object in C# (represented as a Newtonsoft.Json.Linq.JObject object) and I need to flatten it to a dictionary. Let me show you an example of what I mean:

<
5条回答
  •  佛祖请我去吃肉
    2020-12-08 08:06

    JObject jsonObject=JObject.Parse(theJsonString);
    IEnumerable jTokens = jsonObject.Descendants().Where(p => p.Count() == 0);
    Dictionary results = jTokens.Aggregate(new Dictionary(), (properties, jToken) =>
                        {
                            properties.Add(jToken.Path, jToken.ToString());
                            return properties;
                        });
    

    I had the same requirement of flattening a nested json structure to a dictionary object. Found the solution here.

提交回复
热议问题