Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从文件读取json并反序列化总是返回null_C#_Json_Streamreader - Fatal编程技术网

C# 从文件读取json并反序列化总是返回null

C# 从文件读取json并反序列化总是返回null,c#,json,streamreader,C#,Json,Streamreader,我从一个文件中读取Json,当我尝试使用Newtonsoft Json反序列化它时,它返回null。我正在从构建JSON类 . 我不确定为什么它会说null,因为它在使用StreamReader时引入了\n、\r等特殊符号。请帮忙 Json(我使用JsonLint及其有效Json进行了检查) C代码(摘自json2csharp) 公共类机器学习功能 { 公共字符串函数{get;set;} 公共int ArgCount{get;set;} 公共字符串Arg1{get;set;} 公共字符串arg1

我从一个文件中读取Json,当我尝试使用Newtonsoft Json反序列化它时,它返回null。我正在从构建JSON类 . 我不确定为什么它会说null,因为它在使用StreamReader时引入了\n、\r等特殊符号。请帮忙

Json(我使用JsonLint及其有效Json进行了检查)

C代码(摘自json2csharp)

公共类机器学习功能
{
公共字符串函数{get;set;}
公共int ArgCount{get;set;}
公共字符串Arg1{get;set;}
公共字符串arg1类型{get;set;}
公共字符串Arg2{get;set;}
公共字符串Arg2Type{get;set;}
公共字符串返回{get;set;}
公共字符串返回类型{get;set;}
}
公共类数学函数
{
公共字符串函数{get;set;}
公共int ArgCount{get;set;}
公共字符串Arg1{get;set;}
公共字符串arg1类型{get;set;}
公共字符串Arg2{get;set;}
公共字符串Arg2Type{get;set;}
公共字符串返回{get;set;}
公共字符串返回类型{get;set;}
}
公共类根对象
{
公共列表机器学习函数{get;set;}
公共列表函数{get;set;}
}
此json存储在一个文件中,我的阅读如下:当我保留断点时,它会通过引入一些特殊字符来读取字符串,如\n、\r等。但是当我尝试反序列化断点时,断点显示为null,并且在遍历列表时会出现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();
}
string json=string.Empty;
使用(StreamReader=newstreamreader(@“C:\Users\Nikh\OneDrive\Documents\Application/json.txt”))
{
json=reader.ReadToEnd();
}
ParseAndConstructionJSON(json);
public void ParseAndConstructJson(字符串json)//使用Newtonsoft json
{
rootobjectobj=JsonConvert.DeserializeObject(json);
foreach(对象机器学习功能中的变量项)
{
MessageBox.Show(item.Function);
}//DataGrid dg=新DataGrid();
}

您需要使用
JsonProperty
RootObject
中定义属性,以将它们映射到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; }
}
公共类根对象
{
[JsonProperty(PropertyName=“机器学习功能”)]
公共列表机器学习函数{get;set;}
[JsonProperty(PropertyName=“数学函数”)]
公共列表函数{get;set;}
}
从以下位置删除空格:
JSON文件中的“机器学习函数”和“数学函数”,它将毫无问题地反序列化对象。

@nikhil如果有帮助,您可以将答案标记为正确。
 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();
}
public class RootObject
{
    [JsonProperty(PropertyName ="Machine Learning Functions")]
    public List<MachineLearningFunction> MachineLearningFunctions { get; set; }

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