从文件中读取 json 并始终反序列化 returns null

Reading json from file and deserializing always returns null

我正在从一个文件中读取 Json,当我尝试使用 Newtonsoft Json 对其进行反序列化时,它 returns 为空。我正在从 http://json2csharp.com/ 构建 JSON class .我不确定为什么它说 null 是因为它在使用 StreamReader 时引入了特殊符号,如 \n、\r 等。请帮忙。

Json(我检查了 JsonLint 及其有效 json)

{
    "Machine Learning Functions": [{
        "Function": "JaccardDistance",
        "ArgCount": 2,
        "Arg1": "Point1",
        "Arg1Type": "Point",
        "Arg2": "Point2",
        "Arg2Type": "Point",
        "Return": "distance",
        "ReturnType": "Double"
    }],
    "Math Functions": [{
        "Function": "Cosine",
        "ArgCount": 2,
        "Arg1": "document1",
        "Arg1Type": "String",
        "Arg2": "document2",
        "Arg2Type": "String",
        "Return": "angle",
        "ReturnType": "Integer"
    }, {
        "Function": "SQRT",
        "ArgCount": 1,
        "Arg1": "SomeNumber",
        "Arg1Type": "Integer",
        "Return": "Number",
        "ReturnType": "Integer"
    }]

}

C# 代码(取自 json2csharp)

 public class MachineLearningFunction
    {
        public string Function { get; set; }
        public int ArgCount { get; set; }
        public string Arg1 { get; set; }
        public string Arg1Type { get; set; }
        public string Arg2 { get; set; }
        public string Arg2Type { get; set; }
        public string Return { get; set; }
        public string ReturnType { get; set; }
    }

    public class MathFunction
    {
        public string Function { get; set; }
        public int ArgCount { get; set; }
        public string Arg1 { get; set; }
        public string Arg1Type { get; set; }
        public string Arg2 { get; set; }
        public string Arg2Type { get; set; }
        public string Return { get; set; }
        public string ReturnType { get; set; }
    }

    public class RootObject
    {
        public List<MachineLearningFunction> MachineLearningFunctions { get; set; }
        public List<MathFunction> MathFunctions { get; set; }
    }

这个 json 存储在一个文件中,当我保持一个断点时,我正在读取如下内容,它通过引入一些特殊字符(如 \n、\r 等)来读取字符串。但是当我尝试反序列化断点显示 null 并且在遍历列表时出现空引用异常。

 string json = string.Empty;
            using (StreamReader reader = new StreamReader(@"C:\Users\Nikh\OneDrive\Documents\Application/json.txt"))
            {
                json = reader.ReadToEnd();
            }
 ParseAndConstructJson(json);

public void ParseAndConstructJson(string json) //Using Newtonsoft json
        {
            RootObject obj = JsonConvert.DeserializeObject<RootObject>(json);
            foreach (var item in obj.MachineLearningFunctions)
            {
                MessageBox.Show(item.Function);
            }//DataGrid dg = new DataGrid();
}

您需要使用 JsonPropertyRootObject 中定义您的属性,以将它们映射到 json 文件。

public class RootObject
{
    [JsonProperty(PropertyName ="Machine Learning Functions")]
    public List<MachineLearningFunction> MachineLearningFunctions { get; set; }

    [JsonProperty(PropertyName ="Math Functions")]
    public List<MathFunction> MathFunctions { get; set; }
}

删除空格: "Machine Learning Functions" 和 "Math Functions" 在 JSON 文件中,它将毫无问题地反序列化您的对象。