反序列化这个JSON响应到C#
问题描述:
我有这个特定的JSON响应,我试图反序列化没有成功。我希望有人能帮助我。反序列化这个JSON响应到C#
这里是JSON响应我得到:
{
"num_locations": 1,
"locations": {
"98765": {
"street1": "123 Fake Street",
"street2": "",
"city": "Lawrence",
"state": "Kansas",
"postal_code": "66044",
"s_status": "20",
"system_state": "Off"
}
}
}
我用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; }
}
但是,当我尝试使用它我代码:
var locationResponse = JsonConvert.DeserializeObject<RootObject>(response.Content);
我得到的是(观察):
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
显然我不是创造(json2csharp)右侧类的DeserializeObject,而黯然我有过JSON响应(供应商= SimpliSafe)没有控制权。
很明显,“98765”是一个值(位置编号),但json2csharp使它成为__invalid_type__98765类,这可能是为什么它变为空。
任何想法应该如何寻找这个特定的JSON被成功反序列化?
谢谢! Zachs
答
你应该能够用字典来做到这一点:
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; }
}
答
你有Visual Studio中的非快速版本?如果是这样,请将JSON复制到剪贴板,然后转到Visual Studio菜单:编辑>>选择性粘贴>>将JSON粘贴为类。
采用能提供:
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; }
}
这表明你的JSON的结构是不完全正确。
答
您还可以通过属性指定属性名使用来解决这个问题:
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; }
}
...但它真的会更好,如果JSON是正确的格式,使得位置实际上是一个位置对象数组。
非常感谢! – 2015-03-31 19:39:16