简体   繁体   English

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

[英]How to parse / deserialize JSON returned from rest service in C#

I am getting JSON in string format from a URL whose structure is like this, but I am unable to parse it. 我从一个结构是这样的URL获取字符串格式的JSON,但我无法解析它。 It's throwing an exception, any idea how to parse it? 这是一个例外,任何想法如何解析它?

Here is the structure: 这是结构:

{
   "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"
            }
         ]
      }
   }
}

i am doing like this: 我这样做:

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

and

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

and

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

here is my parsing code: 这是我的解析代码:

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

string json = reader.ReadToEnd();

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

objPathway = Jsonobject.pathway;

The classes into which you're deserializing are incorrect. 您要反序列化的类是不正确的。 You are missing the "Patients" class. 你错过了“患者”课程。

When I have JSON and am trying to deserialize it I like to use http://json2csharp.com to generate a first cut at the deserialization classes. 当我有JSON并且我试图反序列化它时,我喜欢使用http://json2csharp.com在反序列化类中生成第一个剪切。 It's more accurate than trying to do it by hand. 它比手工操作更准确。

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

PS The exception you were getting is usually a really good clue that there's something wrong with the way you've defined the deserialization classes. PS你得到的例外通常是一个非常好的线索,你定义反序列化类的方式有问题。

Cannot deserialize the current JSON object (eg {"name":"value"}) into type 'System.Collections.Generic.List`1[ConsoleApplication1.Program+patient]' because the type requires a JSON array (eg [1,2,3]) to deserialize correctly. 无法将当前JSON对象(例如{“name”:“value”})反序列化为类型'System.Collections.Generic.List`1 [ConsoleApplication1.Program + patient]',因为该类型需要JSON数组(例如[1, 2,3])正确反序列化。

To fix this error either change the JSON to a JSON array (eg [1,2,3]) or change the deserialized type so that it is a normal .NET type (eg not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. 要修复此错误,请将JSON更改为JSON数组(例如[1,2,3])或更改反序列化类型,使其成为普通的.NET类型(例如,不是像整数这样的基本类型,而不是类似的集合类型可以从JSON对象反序列化的数组或List。 JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. JsonObjectAttribute也可以添加到类型中以强制它从JSON对象反序列化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM