c# - 如何在 C# 中取消扁平化的 json

标签 c# json flatten

从这个Answer我学习了如何在 C# 中展平 JSON 对象。 来自 JSON 字符串:

{"menu": {
  "id": "file",
  "value": "File",
  "popup": {
    "menuitem": [
      {"value": "New", "onclick": "CreateNewDoc()"},
      {"value": "Open", "onclick": "OpenDoc()"},
      {"value": "Close", "onclick": "CloseDoc()"}
    ]
  }
}}

收件人:

下面是几行字符串,不是对象

menu.id:file
menu.value:File
menu.popup.menuitem[0].value:New
menu.popup.menuitem[0].onclick:CreateNewDoc()
menu.popup.menuitem[1].value:Open
menu.popup.menuitem[1].onclick:OpenDoc()
menu.popup.menuitem[2].value:Close
menu.popup.menuitem[2].onclick:CloseDoc()

现在,我想逆转这个过程。 我可以从这个 question 中找到实现但它是在 JavaScript 中。

如何使用 json.net 在 C# 中展开(从行返回结构化 JSON)?

最佳答案

我设法解决了。

下面是我的代码结合Sarath Rachuri's flattening code .

我没有在太多情况下测试它,所以它可能是错误的。

using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace JSONHelper
{
    class JSONFlattener
    {
        private enum JSONType{
            OBJECT, ARRAY
        }
        public static Dictionary<string, string> Flatten(JObject jsonObject)
        {
            IEnumerable<JToken> jTokens = jsonObject.Descendants().Where(p => p.Count() == 0);
            Dictionary<string, string> results = jTokens.Aggregate(new Dictionary<string, string>(), (properties, jToken) =>
            {
                properties.Add(jToken.Path, jToken.ToString());
                return properties;
            });
            return results;
        }

        public static JObject Unflatten(IDictionary<string, string> keyValues)
        {
            JContainer result = null;
            JsonMergeSettings setting = new JsonMergeSettings();
            setting.MergeArrayHandling = MergeArrayHandling.Merge;
            foreach (var pathValue in keyValues)
            {
                if (result == null)
                {
                    result = UnflatenSingle(pathValue);
                }
                else
                {
                    result.Merge(UnflatenSingle(pathValue), setting);
                }
            }
            return result as JObject;
        }

        private static JContainer UnflatenSingle(KeyValuePair<string, string> keyValue)
        {
            string path = keyValue.Key;
            string value = keyValue.Value;
            var pathSegments = SplitPath(path);

            JContainer lastItem = null;
            //build from leaf to root
            foreach (var pathSegment in pathSegments.Reverse())
            {
                var type = GetJSONType(pathSegment);
                switch (type)
                {
                    case JSONType.OBJECT:
                        var obj = new JObject();
                        if (null == lastItem)
                        {
                            obj.Add(pathSegment,value);
                        }
                        else
                        {
                            obj.Add(pathSegment,lastItem);
                        }
                        lastItem = obj;
                        break;
                    case JSONType.ARRAY:
                        var array = new JArray();
                        int index = GetArrayIndex(pathSegment);
                        array = FillEmpty(array, index);
                        if (lastItem == null)
                        {
                            array[index] = value;
                        }
                        else
                        {
                            array[index] = lastItem;
                        }
                        lastItem = array;
                        break;
                }
            }
            return lastItem;
        }

        public static IList<string> SplitPath(string path){
            IList<string> result = new List<string>();
            Regex reg = new Regex(@"(?!\.)([^. ^\[\]]+)|(?!\[)(\d+)(?=\])");
            foreach (Match match in reg.Matches(path))
            {
                result.Add(match.Value);
            }
            return result;
        }

        private static JArray FillEmpty(JArray array, int index)
        {
            for (int i = 0; i <= index; i++)
            {
                array.Add(null);
            }
            return array;
        }

        private static JSONType GetJSONType(string pathSegment)
        {
            int x;
            return int.TryParse(pathSegment, out x) ? JSONType.ARRAY : JSONType.OBJECT;
        }

        private static int GetArrayIndex(string pathSegment)
        {
            int result;
            if (int.TryParse(pathSegment, out result))
            {
                return result;
            }
            throw new Exception("Unable to parse array index: " + pathSegment);
        }

    }
}

关于c# - 如何在 C# 中取消扁平化的 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40541842/

相关文章:

python - 如何在 Keras 中展平不同大小的数据并在下一层中使用它

c# - 计算非空矩阵元素的数量

c# - 如何序列化自定义 EventHandler

c# - 有人可以用简单的英语或伪代码解释三元运算符吗?

json - 使用 PowerShell for Packer 编辑 JSON

scala - Scala中的嵌套与展平模式匹配

c# - 从控制台在新线程上创建表单

javascript - 如何获取在第一页创建的另一个 php 文件上创建的 JavaScript 对象?

javascript - 根据多个过滤器从 json 数据计算平均值

python - 将字典转换为数据框,键作为列名,键的值作为数据框的列值