C# 如何在C中解析/反序列化rest服务返回的JSON#

C# 如何在C中解析/反序列化rest服务返回的JSON#,c#,.net,json,rest,json-deserialization,C#,.net,Json,Rest,Json Deserialization,我从如下结构的URL获取字符串格式的JSON,但我无法解析它。它抛出了一个异常,知道如何解析它吗 结构如下: { "pathway":{ "patients":{ "patient":[ { "patientid":"7703176", "name":"Abbot, Bud", "status":"Invited",

我从如下结构的URL获取字符串格式的JSON,但我无法解析它。它抛出了一个异常,知道如何解析它吗

结构如下:

{
   "pathway":{
      "patients":{
         "patient":[
            {
               "patientid":"7703176",
               "name":"Abbot, Bud",
               "status":"Invited",
               "start":"2013-12-07",
               "last":"N/A",
               "engagement":"N/A",
               "drug":"N/A",
               "adherence":"N/A",
               "vitals":"Current",
               "last_appointment":"2013-10-25",
               "next_appointment":"None"
            },
            {
               "patientid":"5089554",
               "name":"Brennan, Bonnie",
               "status":"Connected",
               "start":"2013-12-29",
               "last":"2014-02-01",
               "engagement":"Low",
               "drug":" ",
               "adherence":" ",
               "vitals":"Out of Date",
               "last_appointment":"2013-04-21",
               "next_appointment":"None"
            }
         ]
      }
   }
}
我是这样做的:

public class PathWayWrapper
{
    public pathway pathway { get; set; }
}

这是我的解析代码:

StreamReader reader = new StreamReader(response.GetResponseStream());

string json = reader.ReadToEnd();

var Jsonobject = JsonConvert.DeserializeObject<PathWayWrapper>(json);

objPathway = Jsonobject.pathway;
StreamReader=newstreamreader(response.GetResponseStream());
字符串json=reader.ReadToEnd();
var Jsonobject=JsonConvert.DeserializeObject(json);
objPathway=Jsonobject.pathway;

要反序列化到的类不正确。你错过了“病人”课

当我有JSON并试图反序列化它时,我喜欢使用它来生成反序列化类的第一个剪切。这比用手做要准确得多

var jsonobject = JsonConvert.DeserializeObject<RootObject>(json);

public class Patient
{
    public string patientid { get; set; }
    public string name { get; set; }
    public string status { get; set; }
    public string start { get; set; }
    public string last { get; set; }
    public string engagement { get; set; }
    public string drug { get; set; }
    public string adherence { get; set; }
    public string vitals { get; set; }
    public string last_appointment { get; set; }
    public string next_appointment { get; set; }
}

public class Patients
{
    public List<Patient> patient { get; set; }
}

public class Pathway
{
    public Patients patients { get; set; }
}

public class RootObject
{
    public Pathway pathway { get; set; }
}
var jsonobject=JsonConvert.DeserializeObject(json); 公立病人 { 公共字符串patientid{get;set;} 公共字符串名称{get;set;} 公共字符串状态{get;set;} 公共字符串开始{get;set;} 公共字符串last{get;set;} 公共字符串参与{get;set;} 公共字符串drug{get;set;} 公共字符串{get;set;} 公共字符串vitals{get;set;} 公共字符串last_约会{get;set;} 公共字符串next_约会{get;set;} } 公营病人 { 公共列表患者{get;set;} } 公共班级通道 { 公共病人{get;set;} } 公共类根对象 { 公共路径{get;set;} } 另外,您得到的异常通常是一个很好的线索,表明您定义反序列化类的方式有问题

无法将当前JSON对象(例如{“名称”:“值”})反序列化为“System.Collections.Generic.List`1[ConsoleApplication1.Program+patient]”类型,因为该类型需要JSON数组(例如[1,2,3])才能正确反序列化

若要修复此错误,请将JSON更改为JSON数组(例如[1,2,3]),或更改反序列化类型,使其成为可以从JSON对象反序列化的正常.NET类型(例如,不是integer之类的基元类型,也不是数组或列表之类的集合类型)。还可以将JsonObjectAttribute添加到类型中,以强制它从JSON对象反序列化


例外情况是什么?我格式化了你的JSON,但它的格式似乎不正确。你的JSON在“]}}}末尾缺少这些,你需要关闭数组、对象、对象。我已经更正了它,但仍然无法解析。有人能告诉我如何解析它吗?你缺少数组的右括号:1
]
,对于
患者
路径
和总体范围,分别为3个
}
StreamReader reader = new StreamReader(response.GetResponseStream());

string json = reader.ReadToEnd();

var Jsonobject = JsonConvert.DeserializeObject<PathWayWrapper>(json);

objPathway = Jsonobject.pathway;
var jsonobject = JsonConvert.DeserializeObject<RootObject>(json);

public class Patient
{
    public string patientid { get; set; }
    public string name { get; set; }
    public string status { get; set; }
    public string start { get; set; }
    public string last { get; set; }
    public string engagement { get; set; }
    public string drug { get; set; }
    public string adherence { get; set; }
    public string vitals { get; set; }
    public string last_appointment { get; set; }
    public string next_appointment { get; set; }
}

public class Patients
{
    public List<Patient> patient { get; set; }
}

public class Pathway
{
    public Patients patients { get; set; }
}

public class RootObject
{
    public Pathway pathway { get; set; }
}