簡體   English   中英

RestSharp反序列化不起作用

[英]RestSharp Deserialization not working

下面的原始輸出(從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; }
}

我真的不知道為什么要向您提供這個答案。 但是我還是。 您應該通過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響應列表”:{ “@油門”: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“}}}

如果您使用下面的類

[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