如何在C#中将json转换为平面结构 [英] How to convert json to flat structure in C#

查看:99
本文介绍了如何在C#中将json转换为平面结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用C#编写将JSON转换为键/值对的函数.它应该支持数组.因此,例如以下JSON:

I'm trying to write function in C# that will converts JSON to a key/value pairs. It should support arrays. So for example the following JSON:

{ 
    title: title_value,
    components: [
        {
            component_id: id1,
            menu: [
                   {title: menu_title1},
                   {title: menu_title_x},
                   {id: menu_id1}    
            ]
        },
        {
             component_id: id2,
             menu: [
                   {title: menu_title2},
                   {id: menu_id2}    
             ]
        }
    ]
}

应转换为:

  • title = title_value
  • components.0.component_id = id1
  • components.0.menu.0.title = menu_title1
  • components.0.menu.1.title = menu_title_x
  • components.0.menu.2.id = menu_id1
  • components.1.component_id = id2
  • components.1.menu.0.title = menu_title2
  • components.1.menu.1.id = menu_id2

执行此任务有任何简单的方法吗?当我开始考虑数组和嵌套数组时,逻辑变得复杂.

Is it any simple way to do this task? The logic becomes complicated when I start taking into account arrays and nested arrays.

推荐答案

解决方案如下. JavaScriptSerializer从json字符串('json')创建对象('o'),然后BuildVariablesList方法遍历该对象并填充包含结果的字典('additionalParameters').

The solution for this is as following. JavaScriptSerializer creates object ('o') from json string ('json'), than method BuildVariablesList traverse the object and populates dictionary ('additionalParameters') that contains results.

    var jss = new JavaScriptSerializer();
    var o = return new DynamicJsonObject(jss.Deserialize<Dictionary<string, object>>(json));

    var additionalParameters = new Dictionary<string, string>();
    BuildVariablesList(o.GetInternalDictionary(), "", additionalParameters);

    private static string AppendToPathString (string path, object part )
    {
        return path.Trim().Length == 0 ? part.ToString() : path + '.' + part;
    }

    public static void BuildVariablesList(object obj, string path, Dictionary<string, string> result)
    {
        if ( obj is ArrayList)
        {
            var arrayObj = obj as ArrayList;
            for (var i = 0; i<arrayObj.Count; i++ )
            {
                BuildVariablesList(arrayObj[i], AppendToPathString(path,i), result);
            }
        }else if ( obj is Dictionary<string, object>)
        {
            var dictObject = obj as Dictionary<string, object>;
            foreach (var entry in dictObject)
            {
                if (entry.Value is String && (path.Trim().Length > 0 || !ReservedFieldNames.Contains( entry.Key.ToLower())))
                {
                    result.Add(AppendToPathString(path,entry.Key), entry.Value as String);
                }
                else if (entry.Value is Dictionary<string, object>)
                {
                    BuildVariablesList(entry.Value as Dictionary<string, object>, AppendToPathString(path, entry.Key), result);
                }
                else if (entry.Value is ArrayList)
                {
                    BuildVariablesList(entry.Value as ArrayList, AppendToPathString(path, entry.Key), result);
                }
            }
        }            
    }

这篇关于如何在C#中将json转换为平面结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆