C#JSON.net null嵌套对象

时间:2016-11-21 23:04:35

标签: c# json nested json.net

我正在将一些JSON解析为csv。我的班级设置如下:

public class SearchObj
{ 
    public class Property
    {           
        public string __cloudera_internal__hueLink { get; set; }
    }
    public class root
    {
        public string blockSize { get; set; }
        public string clusteredByColNames { get; set; }
        public string compressed { get; set; }
        public string created { get; set; }
        public string dataType { get; set; }
        public string deleted { get; set; }
        public Property properties { get; set; }
    }
}

然后我会使用这个对象:

string json = infile.ReadToEnd();
var rootObject = JsonConvert.DeserializeObject<List<SearchObj.root>>(json);
for (int i = 0; i < rootObject.Count; i++)
{
    //holding is a dict that holds json key/value pairs, 
    //arg[1] is a prefix for key:value pairs, in this case, no prefix
    //so a nested key goes into dictionary as key.nestedkey
    ResolveTypeAndValue(rootObject[i], "", holding);
}

当然,我们需要查看Resolve方法的代码:

private static void ResolveTypeAndValue(object obj, string name, Dictionary<string, string> storage)
{
    //this next line is the error problem
    var type = obj.GetType();
    foreach (var p in type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance))
    {
        if (p.PropertyType.IsClass && p.PropertyType != typeof(string))
        {
            var currentObj = p.GetValue(obj);
            ResolveTypeAndValue(currentObj, p.Name + ".", storage);
        }
        else
        {
            string val = "";
            if (p.GetValue(obj) != null)
            {
                val = p.GetValue(obj).ToString();
            }
            storage.Add(name + p.Name, val);
        }
    }
}

所以,当我运行这个时,我得到一个“未处理的类型'System.NullReferenceException'的异常'.....'标记上面的行。我相信这是因为大多数时候,“属性”键是null。事实上,“properites”键尽管嵌套只有一个键:“__cloudera_internal__hueLink”的值对作为键,而html地址(本质上是文本)作为值。

我在一组不同的JSON架构上成功运行了这段代码而没有任何问题 - 但对于我的生活,我无法弄清楚如何基本上将键/值对设置为类似“属性”的东西:“null”或“properties .__ cloudera_internal__hueLink”:“值字段”。以前嵌套的json对象总是有一些东西,即使它从入口到入口都不一样。

你可能需要在新手级别提供帮助,我只在工作中使用C#,因为它是我所有的 - 但显然我已经在这里选择了一两件关于处理JSON对象的事情,但这个让我陷入困境

我要么得到错误,要么根本没有填充“属性”并抛出一个未找到错误的错误

1 个答案:

答案 0 :(得分:0)

ResolveTypeAndValue功能更改为:

private static void ResolveTypeAndValue(object obj, string name, Dictionary<string, string> storage)
{
    if (obj == null)
    {
        storage.Add(name, null);
        return;
    }

    var type = obj.GetType();
    foreach (var p in type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance))
    {
        if (p.PropertyType.IsClass && p.PropertyType != typeof(string))
        {
            var currentObj = p.GetValue(obj);
            ResolveTypeAndValue(currentObj, p.Name, storage); // removed this: '+ "."'
        }
        else
        {
            string val = "";
            if (p.GetValue(obj) != null)
            {
                val = p.GetValue(obj).ToString();
            }
            storage.Add(name + "." + p.Name, val); // added this: '+ "."'
        }
    }
}