下面的原始输出(从 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. "&" => "&" 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-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":"下一步","@href":"hxxp://ipaddress/spectrum/restful/devices?id=93fc1a07-60be-4dd5-964c-7e8660dd3028&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; }
}