反序列化此 JSON 对 C# 的响应
Deserializing this JSON response to C#
我有这个特定的 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 并获得了这些推荐 classes:
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) 正确的 classes,遗憾的是我无法控制 JSON 响应(供应商 = SimpliSafe)。
很明显,“98765”是一个值(位置编号),但 json2csharp 将其放入此 __invalid_type__98765 class 中,这可能就是它为空的原因。
知道 classes 应该如何寻找这个特定的 JSON 才能成功反序列化吗?
谢谢!
扎克斯
你应该可以用字典来做到这一点:
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 的非 Express 版本吗?如果是这样,将 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 的格式正确,这样 Locations 实际上是一个位置对象数组,那就更好了。
我有这个特定的 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 并获得了这些推荐 classes:
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) 正确的 classes,遗憾的是我无法控制 JSON 响应(供应商 = SimpliSafe)。
很明显,“98765”是一个值(位置编号),但 json2csharp 将其放入此 __invalid_type__98765 class 中,这可能就是它为空的原因。
知道 classes 应该如何寻找这个特定的 JSON 才能成功反序列化吗?
谢谢! 扎克斯
你应该可以用字典来做到这一点:
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 的非 Express 版本吗?如果是这样,将 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 的格式正确,这样 Locations 实际上是一个位置对象数组,那就更好了。