[英]Deserializing this JSON response to C#
I have this specific JSON response that I am trying to deserialize without success. 我有这个特定的JSON响应,我试图反序列化但未成功。 I am hoping someone can help me.
我希望有人能帮助我。
Here is the JSON response I get: 这是我得到的JSON响应:
{
"num_locations": 1,
"locations": {
"98765": {
"street1": "123 Fake Street",
"street2": "",
"city": "Lawrence",
"state": "Kansas",
"postal_code": "66044",
"s_status": "20",
"system_state": "Off"
}
}
} }
I used json2csharp http://json2csharp.com and got these recommended classes: 我使用json2csharp http://json2csharp.com并获得了以下推荐的类:
public class __invalid_type__98765
{
public string street1 { get; set; }
public string street2 { get; set; }
public string city { get; set; }
public string state { get; set; }
public string postal_code { get; set; }
public string s_status { get; set; }
public string system_state { get; set; }
}
public class Locations
{
public __invalid_type__98765 __invalid_name__98765 { get; set; }
}
public class RootObject
{
public int num_locations { get; set; }
public Locations locations { get; set; }
}
But when I try to use it in my code: 但是当我尝试在代码中使用它时:
var locationResponse = JsonConvert.DeserializeObject<RootObject>(response.Content);
What I get is (Watch): 我得到的是(观看):
locationResponse : {RestSharpConsoleApplication.Program.RootObject} : RestSharpConsoleApplication.Program.RootObject
locations : {RestSharpConsoleApplication.Program.Locations} : RestSharpConsoleApplication.Program.Locations
__invalid_name__98765 : null : RestSharpConsoleApplication.Program.__invalid_type__98765
num_locations : 1 : int
Obviously I am not creating (json2csharp) the right classes for the DeserializeObject, and sadly I have no control over the JSON response (vendor = SimpliSafe). 显然,我没有为DeserializeObject创建(json2csharp)正确的类,但遗憾的是,我无法控制JSON响应(供应商= SimpliSafe)。
It is obvious the "98765" is meant to be a value (location number) but json2csharp makes it into this __invalid_type__98765 class and this is probably why it gets null. 显而易见,“ 98765”是一个值(位置编号),但是json2csharp使其成为__invalid_type__98765类,这可能就是为什么它为null的原因。
Any idea how should the classes look for this particular JSON to be successfully deserialized? 知道这些类如何查找要成功反序列化的特定JSON吗?
Thanks! 谢谢! Zachs
扎克斯
You should be able to do this with a dictionary: 您应该可以使用字典来做到这一点:
public class MyData{
[JsonProperty("locations")]
public Dictionary<string, Location> Locations {get;set;}
}
public class Location
{
public string street1 { get; set; }
public string street2 { get; set; }
public string city { get; set; }
public string state { get; set; }
public string postal_code { get; set; }
public string s_status { get; set; }
public string system_state { get; set; }
}
Do you have a non-Express version of Visual Studio? 您是否有Visual Studio的非Express版本? If so, copy the JSON to clipboard and then go to the Visual Studio menu: Edit >> Paste special >> Paste JSON as classes.
如果是这样,请将JSON复制到剪贴板,然后转到Visual Studio菜单:编辑>>选择性粘贴>>将JSON作为类粘贴。
Using that gives: 使用它可以得到:
public class Rootobject {
public int num_locations { get; set; }
public Locations locations { get; set; }
}
public class Locations {
public _98765 _98765 { get; set; }
}
public class _98765 {
public string street1 { get; set; }
public string street2 { get; set; }
public string city { get; set; }
public string state { get; set; }
public string postal_code { get; set; }
public string s_status { get; set; }
public string system_state { get; set; }
}
That suggests your JSON structure is not quite right. 这表明您的JSON结构不太正确。
You can also specify the property name via an attribute to use to get around this: 您也可以通过属性指定属性名称,以解决此问题:
public class RootObject
{
public int num_locations { get; set; }
public Locations locations { get; set; }
}
public class Locations
{
[JsonProperty("98765")]
public LocationInner Inner { get; set; }
}
public class LocationInner
{
public string street1 { get; set; }
public string street2 { get; set; }
public string city { get; set; }
public string state { get; set; }
public string postal_code { get; set; }
public string s_status { get; set; }
public string system_state { get; set; }
}
...but it would really be better if the JSON were properly formatted such that the Locations was actually an array of location objects. ...但是,如果将JSON正确地格式化为“位置”实际上是位置对象数组,那会更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.