我有这个特定的JSON响应,我试图反序列化而没有成功。我希望有人可以帮助我。
以下是我得到的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
显然,我没有为DeserializeObject创建(json2csharp)正确的类,遗憾的是我无法控制JSON响应(vendor = SimpliSafe)。
很明显&#34; 98765&#34;是一个值(位置编号),但json2csharp使它成为__invalid_type__98765类,这可能是它为空的原因。
知道如何让类成功地反序列化这个特定的JSON吗?
谢谢! Zachs
答案 0 :(得分:3)
你应该能够用字典做到这一点:
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; }
}
答案 1 :(得分:-2)
您是否拥有Visual Studio的非Express版本?如果是这样,请将JSON复制到剪贴板,然后转到Visual Studio菜单:编辑&gt;&gt;粘贴特殊&gt;&gt;将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结构不太正确。
答案 2 :(得分:-2)
您还可以通过用于解决此问题的属性指定属性名称:
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格式正确,Locations实际上是一个位置对象数组,那真的会更好。