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

时间:2014-02-21 15:48:47

标签: c# .net json rest json-deserialization

我从字符串格式获取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; }
}

public class pathway
{
    public List<patient> patients { get; set; }
}

public class patient
{
    public long 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; }
}

这是我的解析代码:

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

string json = reader.ReadToEnd();

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

objPathway = Jsonobject.pathway;

1 个答案:

答案 0 :(得分:13)

您要反序列化的类是不正确的。你错过了“患者”课程。

当我有JSON并尝试反序列化时,我喜欢使用http://json2csharp.com在反序列化类中生成第一个剪切。它比手工操作更准确。

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; }
}

P.S。你得到的例外通常是一个非常好的线索,你定义反序列化类的方式有问题。

  

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

     

要修复此错误,请将JSON更改为JSON数组(例如[1,2,3])或更改反序列化类型,使其成为普通的.NET类型(例如,不是像整数这样的基本类型,而不是可以从JSON对象反序列化的集合类型,如数组或List)。 JsonObjectAttribute也可以添加到类型中以强制它从JSON对象反序列化。