【问题标题】:RestSharp Deserialization not workingRestSharp反序列化不起作用
【发布时间】:2014-11-20 05:42:59
【问题描述】:

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

这是调用时返回的原始 JSON 内容:

{"ns1.model-response-list":{"@throttle":"2","@total-models":"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":{"@rel":"next","@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; }
}

【问题讨论】:

  • 等等,什么?首先你问一个问题,然后在我回复之后,你删除问题并将我的答案(上面的类定义)复制/粘贴到一个新问题中? FTW,真的吗?!

标签: c# deserialization restsharp


【解决方案1】:

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

一旦你有了 Json.NET,你的 JSON 就可以使用下面的类进行反序列化。这一次,我希望您在收到答案后不要删除问题,而是接受并可能投票?

所以,使用

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

控制台输出

{"ns1.model-response-list":{"@throttle":2,"@total-models":3372,"ns1.model-responses":{"ns1.model":[{"@ mh":"0x20e800","ns1.attribute":{"@id":"0x1006e","$":"Sservername.com"}},{"@mh":"0x21a400","ns1.attribute" :{"@id":"0x1006e","$":"servername.com"}}]},"ns1.link":{"@rel":"next","@href":" hxxp://ipaddress/spectrum/restful/devices?id=93fc1a07-60be-4dd5-964c-7 e8660dd3028&start=2&throttlesize=2","@type":"application/xml"}}}

如果你使用下面的类

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

【讨论】:

  • 再次感谢您!很抱歉删除了旧问题,更新了代码的“新”状态有点令人困惑,这就是我这样做的原因。实际上,我应该赞成它然后开始这个新问题。
  • 如果您想继续将 JSON.NET 与 RestSharp 一起使用,您可以轻松地将其与 nuget.org/packages/RestSharp.Newtonsoft.Json 集成。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-11
  • 1970-01-01
相关资源
最近更新 更多