RestSharp反序列化无法正常工作

时间:2014-11-20 05:42:59

标签: c# deserialization restsharp

下面的原始输出(从RestResponse.Content属性获得)未被反序列化。是因为“ns1”被添加为前缀吗?有什么我做错了吗?

以下是拨打电话时返回的原始JSON内容:

  

{ “ns1.model响应列表”:{ “@油门”: “2”, “@总的模型”: “3372”,“ns1.model-RE   sponses “:{” ns1.model “:[{” @ MH “:” 0x20e800" , “ns1.attribute”:{ “@ ID”: “0x1006e”, “$”:“S   servername.com “}},{” @ MH “:” 0x21a400" , “ns1.attribute”:{ “@ ID”:“为0x100   6E “ ”$“: ”servername.com“}}]}, ”ns1.link“:{ ”@相对“: ”下一个“, ”@ HREF“:”   http:// ipaddress / spectrum / restful / devices?id = 93fc1a07-60be-4dd5-964c-7 e8660dd3028& start = 2& throttlesize = 2“,”@ type“:”application / xml“}}}

class Program
{
static void Main(string[] args)
{
var client = new RestClient(Spectrum.Endpoints.Development);
client.Authenticator = new HttpBasicAuthenticator("myid", "mypassword");

var request = new RestRequest(Method.GET);
request.RequestFormat = DataFormat.Xml;
request.Resource = "devices?{attr}&{throttlesize}";
request.AddParameter("attr", Spectrum.Attributes.ModelName);
request.AddParameter("throttlesize", "2");

IRestResponse<ModelResponseList> response = client.Execute<ModelResponseList>(request);

Console.Write(response.Data.Throttle); // This line keeps returning 0, but should return 2
}

以下是应该保存数据的类:

[DeserializeAs(Name = "model-response-list")]
public class ModelResponseList
{
    [DeserializeAs(Name = "throttle")]
    public int Throttle { get; set; }

    [DeserializeAs(Name = "total-models")]
    public int TotalModels { get; set; }

    [DeserializeAs(Name = "model-responses")]
    public List<Model> ModelResponses { get; set; }

    [DeserializeAs(Name = "link")]
    public Link Link { get; set; }
}

public class Model
{
    public string Mh { get; set; }
    public ModelAttribute Attribute { get; set; }
}

public class ModelAttribute
{
    public string Id { get; set; }
    public string Value { get; set; }
}

public class Link
{
    public string Rel { get; set; }
    // Note! Href must be escaped, e.g. "&" => "&amp;" or comment this prop out
    public string Href { get; set; }
    public string Type { get; set; }
}

1 个答案:

答案 0 :(得分:2)

我真的不知道为什么我会向你提供这个答案。但是,我仍然。 您应该通过NuGet获取Json.NET并让它帮助您。它比RestSharp的内置解/序列化更复杂。

一旦你有了Json.NET,那么你的JSON可以使用下面的类进行解序/序列化。这一次,我希望你在收到答案后不要删除这个问题,但是接受并且可能反过来支持它?

所以,使用

var response = client.Execute(request);
var deserialized = JsonConvert.DeserializeObject<Wrapper>(response.Content);
Console.WriteLine(JsonConvert.SerializeObject(deserialized));

输出以控制以下

  

{&#34; ns1.model响应列表&#34; {&#34; @节气门&#34;:2&#34; @总的模型&#34;:3372,&#34; ns1.model - 响应&#34; {&#34; ns1.model&#34;:[{&#34; @ MH&#34;:&#34; 0x20e800&#34;&#34; ns1.attribute&# 34;:{&#34; @ ID&#34;:&#34; 0x1006e&#34;&#34; $&#34;:&#34; Sservername.com&#34;}},{&#34 ; @ MH&#34;:&#34; 0x21a400&#34;&#34; ns1.attribute&#34; {&#34; @ ID&#34;:&#34; 0x1006e&#34;&#34 ; $&#34;:&#34; servername.com&#34;}}]},&#34; ns1.link&#34; {&#34; @相对&#34;:&#34;下一个&# 34;,&#34; @ HREF&#34;:&#34;   hxxp:// IP地址/频谱/宁静/装置ID = 93fc1a07-60be-4dd5-964c -7-   e8660dd3028&安培;开始= 2及throttlesize = 2&#34;&#34; @类型&#34;:&#34;应用/ XML&#34;}}}

如果您使用以下课程

[JsonObject]
public class Wrapper
{
    [JsonProperty(PropertyName = "ns1.model-response-list")]
    public ModelResponseList ModelResponseList { get; set; }
}

[JsonObject]
public class ModelResponseList
{
    [JsonProperty(PropertyName = "@throttle")]
    public int Throttle { get; set; }

    [JsonProperty(PropertyName = "@total-models")]
    public int TotalModels { get; set; }

    [JsonProperty(PropertyName = "ns1.model-responses")]
    public Responses ModelResponses { get; set; }

    [JsonProperty(PropertyName = "ns1.link")]
    public Link Link { get; set; }
}

[JsonObject]
public class Responses
{
    [JsonProperty(PropertyName = "ns1.model")]
    public List<Model> Model { get; set; }
}

[JsonObject]
public class Model
{
    [JsonProperty(PropertyName = "@mh")]
    public object Mh { get; set; }

    [JsonProperty(PropertyName = "ns1.attribute")]
    public ModelAttribute Attribute { get; set; }
}

[JsonObject]
public class ModelAttribute
{
    [JsonProperty(PropertyName = "@id")]
    public string Id { get; set; }

    [JsonProperty(PropertyName = "$")]
    public string Value { get; set; }
}

[JsonObject]
public class Link
{
    [JsonProperty(PropertyName = "@rel")]
    public string Rel { get; set; }

    [JsonProperty(PropertyName = "@href")]
    public string Href { get; set; }

    [JsonProperty(PropertyName = "@type")]
    public string Type { get; set; }
}