如何将下面的json字符串转换为C中的对象列表#

如何将下面的json字符串转换为C中的对象列表#,json,json.net,Json,Json.net,我正在尝试将下面的json字符串转换为对象列表。我犯了一个错误。你能帮忙吗 string jsonp = @"{ 'data': [ { 'SectionId':1,'Name':'Bachelor ','NavigationRoute':'applicantExam/education','Position':15,IsEducation':true,'IsEducationCollegeDegree':null,'previousSection':null,'nextSection':n

我正在尝试将下面的json字符串转换为对象列表。我犯了一个错误。你能帮忙吗

string jsonp = @"{
  'data': [ { 'SectionId':1,'Name':'Bachelor ','NavigationRoute':'applicantExam/education','Position':15,IsEducation':true,'IsEducationCollegeDegree':null,'previousSection':null,'nextSection':null,'IsCurrent':null,'SectionCompleted':null},
            { 'SectionId':2,'Name':'Master','NavigationRoute':'applicantExam/education','Position':20,'IsEducation':true,'IsEducationCollegeDegree':null,'previousSection':null,'nextSection':null,'IsCurrent':null,'SectionCompleted':null} ] 
   }";

 ExamSectionModel[] m = JsonConvert.DeserializeObject<ExamSectionModel[]>(jsonp);
 foreach( var x in m)
 {
     Console.WriteLine(x.Name);
 }
stringjsonp=@”{
“数据”:[{'SectionId':1,'Name':'Bachester','NavigationRoute':'Appliantexam/education','Position':15,IsEducation':true,'IsEducationCollegeDegree':null,'previousSection':null,'nextSection':null,'IsCurrent':null,'SectionCompleted':null},
{'SectionId':2,'Name':'Master','NavigationRoute':'Appliantexam/education','Position':20,'IsEducation':true,'IsEducationCollegeDegree':null,'previousSection':null,'nextSection':null,'IsCurrent':null,'SectionCompleted':null}]
}";
ExamSectionModel[]m=JsonConvert.DeserializeObject(jsonp);
foreach(变量x,单位:m)
{
Console.WriteLine(x.Name);
}

您的考试部分数据数组在JSON中不在根级别;它位于
数据
属性内,向下一层。要修复此问题,您需要创建一个包装器类,然后反序列化为:

public class RootObject
{
    public ExamSectionModel[] Data { get; set; }
}

public class ExamSectionModel
{
    public int SectionId { get; set; }
    public string Name { get; set; }
    public string NavigationRoute { get; set; }
    public int Position { get; set; }
    public bool IsEducation { get; set; }
    public bool? IsEducationCollegeDegree { get; set; }
    public object previousSection { get; set; }
    public object nextSection { get; set; }
    public bool? IsCurrent { get; set; }
    public bool? SectionCompleted { get; set; }
}
然后:

RootObject root = JsonConvert.DeserializeObject<RootObject>(jsonp);
foreach(var x in root.Data)
{
    Console.WriteLine(x.Name);
}
rootobjectroot=JsonConvert.DeserializeObject(jsonp);
foreach(root.Data中的变量x)
{
Console.WriteLine(x.Name);
}
小提琴:

另一方面,您的JSON在第一行的
'Position':15,
之后和
IsEducation':true之前似乎缺少一个引号。我假设这只是问题中的一个输入错误,但如果不是,您需要在JSON中修复它,否则它将无法解析


另外,为了符合标准,在JSON中应该使用双引号,而不是单引号。(请参阅。)JSON.net可以处理单引号,但其他解析器可能不会这么宽容。

我认为此链接可以帮助您: