C# flattening json structure

后端 未结 5 589
囚心锁ツ
囚心锁ツ 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:11

    You could use the JSONPath $..* to get all members of the JSON structure and filter out the ones with no children to skip the container properties.

    e.g.

    var schemaObject = JObject.Parse(schema);
    var values = schemaObject
        .SelectTokens("$..*")
        .Where(t => !t.HasValues)
        .ToDictionary(t => t.Path, t => t.ToString());
    

提交回复
热议问题