简体   繁体   English

RestSharp反序列化不起作用

[英]RestSharp Deserialization not working

The below raw output (obtained from the RestResponse.Content Property) is not being deserialized. 下面的原始输出(从RestResponse.Content属性获得)没有被反序列化。 Is it because "ns1" is being added as a prefix? 是否因为“ ns1”被添加为前缀? Is there something I am doing wrong? 我做错什么了吗?

Here is the raw JSON content returned when making a call: 这是拨打电话时返回的原始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"}}} {“ 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
}

Here are the classes which should hold the data: 以下是应保存数据的类:

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

I really don't know why I am providing you with this answer. 我真的不知道为什么要向您提供这个答案。 But still, I am. 但是我还是。 You should go and get Json.NET via NuGet and let it help you out. 您应该通过NuGet来获取Json.NET,并让它帮助您。 It's more sophisticated than RestSharp's built-in de/serialization. 它比RestSharp的内置反序列化功能更复杂。

Once you have Json.NET, then your JSON can be de/serialized using the classes below. 一旦有了Json.NET,就可以使用下面的类对JSON进行反序列化。 This time I hope you are NOT removing the question after you receive an answer but accept and maybe upvote it instead? 这次,我希望您在收到答案后不会删除问题,而是接受并可能对其进行投票?

So, using 因此,使用

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

Outputs to console the following 输出到控制台以下

{"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"}}} { “ns1.model响应列表”:{ “@油门”:2 “@总的模型”:3372, “ns1.model - 响应”:{ “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“}}}

If you use the classes below 如果您使用下面的类

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

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

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