如何解析/反序列化JSON的REST服务在C#中返回 [英] How to parse / deserialize JSON returned from rest service in C#

查看:237
本文介绍了如何解析/反序列化JSON的REST服务在C#中返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是从一个URL,其​​结构是这样获得的JSON字符串格式,但我无法解析它。它抛出一个异常,任何想法如何解析呢?

下面是结构:

  {
   路径:{
      病人:{
         患者:[
            {
               patientid:7703176,
               名:方丈,芽,
               状态:请,
               开始:2013年12月7日,
               最后一个:N / A,
               交往:N / A,
               毒:N / A,
               坚持:N / A,
               命脉:当前,
               last_appointment:2013年10月25号,
               next_appointment:无
            },
            {
               patientid:5089554,
               名:布伦南,邦妮,
               状态:已连接,
               开始:2013年12月29日,
               最后一个:2014年2月1日
               交往:低,
               药物: ,
               坚持:,
               命脉:过时,
               last_appointment:2013年4月21日,
               next_appointment:无
            }
         ]
      }
   }
}
 

我这样做是这样的:

 公共类PathWayWrapper
{
    公共途径途径{获得;组; }
}
 

 公共类途径
{
    公开名单<患者GT;患者{获得;组; }
}
 

 公共类病人
{
    众长patientid {获得;组; }
    公共字符串名称{;组; }
    公共字符串状态{获得;组; }
    公共字符串的开始{获得;组; }
    公共字符串最后{获得;组; }
    公共字符串参与{获得;组; }
    公共字符串药物{获得;组; }
    公共字符串遵守{获得;组; }
    公共字符串命脉{获得;组; }
    公共字符串last_appointment {获得;组; }
    公共字符串next_appointment {获得;组; }
}
 

下面是我的分析code:

  StreamReader的读者=新的StreamReader(response.GetResponseStream());

JSON字符串= reader.ReadToEnd();

VAR的JSONObject = JsonConvert.DeserializeObject< PathWayWrapper>(JSON);

objPathway = Jsonobject.pathway;
 

解决方案

在其中你要反序列化的类是不正确的。你缺少的病人级。

在我的JSON,我试图反序列化它,我喜欢用 http://json2csharp.com 生成首先削减在反序列化类。它比试图手工做的更准确。

  VAR的JSONObject = JsonConvert.DeserializeObject< RootObject>(JSON);

公共类病人
{
    公共字符串patientid {获得;组; }
    公共字符串名称{;组; }
    公共字符串状态{获得;组; }
    公共字符串的开始{获得;组; }
    公共字符串最后{获得;组; }
    公共字符串参与{获得;组; }
    公共字符串药物{获得;组; }
    公共字符串遵守{获得;组; }
    公共字符串命脉{获得;组; }
    公共字符串last_appointment {获得;组; }
    公共字符串next_appointment {获得;组; }
}

公共类患者
{
    公开名单<患者GT;患者{获得;组; }
}

公共类路径
{
    公立医院病人患者{获得;组; }
}

公共类RootObject
{
    公共通路途径{获得;组; }
}
 

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

  

无法反序列化的当前JSON对象(例如{名:值})入式'System.Collections.Generic.List`1 [ConsoleApplication1.Program +耐心]',因为此类型需要一个JSON阵列(例如: [1,2,3]),以正确的反序列化。

     

要解决这个错误要么改变的JSON到一个JSON阵列(例如[1,2,3])或更改反序列化的类型,以便它是一个正常的.NET类型(例如,不是一个原始类型类似整数,而不是像一个数组或列表),可以从一个JSON对象反序列化集合类型。 JsonObjectAttribute也可以加入到类型迫使它从JSON对象反序列化

I am getting JSON in string format from a URL whose structure is like this, but I am unable to parse it. 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. 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; }
}

P.S. 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.

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[ConsoleApplication1.Program+patient]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.

这篇关于如何解析/反序列化JSON的REST服务在C#中返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆