【问题标题】:How to parse / deserialize JSON returned from rest service in C#如何在C#中解析/反序列化从rest服务返回的JSON
【发布时间】:2014-03-23 05:35:00
【问题描述】:

我从一个结构类似这样的 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; }
}

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;

【问题讨论】:

  • 有什么例外?
  • 我格式化了您的 JSON,但格式似乎不正确。
  • 你的JSON最后缺少这些"]}}}",你需要关闭数组,对象,对象,对象。
  • 我已经更正了,但我仍然无法解析,谁能告诉我如何解析它
  • 您缺少右大括号:1 个] 用于数组,3 个} 用于patientspathway 和整体范围。

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


【解决方案1】:

您要反序列化的类不正确。您缺少“患者”课程。

当我拥有 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; }
}

附:您得到的异常通常是一个很好的线索,表明您定义反序列化类的方式有问题。

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

要修复此错误,要么将 JSON 更改为 JSON 数组(例如 [1,2,3]),要么将反序列化类型更改为正常的 .NET 类型(例如,不是像整数这样的原始类型,而不是可以从 JSON 对象反序列化的集合类型(如数组或列表)。 JsonObjectAttribute 也可以添加到类型中,以强制它从 JSON 对象反序列化。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-21
    • 2021-06-04
    • 1970-01-01
    • 2020-09-22
    • 2019-01-04
    • 1970-01-01
    相关资源
    最近更新 更多