[英]Facing issues deserializing JSON into .NET
我是处理JSON的新手。 但是已经成功创建了一些类,这些类恰好返回了我想要的。 但是,这种回应让我感到困惑。
我已经查看并尝试了网站上的各种示例,以将JSON响应转换为类。 但是,我继续遇到运行时错误。 任何关于我要去哪里的指示都将不胜感激。
JSON回应
{
"locationResponse": {
"locations": [
"E911AID:93a6:2db4:0589:261d,streetDir:NE,street:89th,zip:98052,city:Redmond,streetNameSuffix:St,name:Jonathon Doe,state:WA,houseNum:23619",
"E911AID:93a6:2db4:0589:261d,streetDir:NE,street:89th,zip:98052,city:Redmond,streetNameSuffix:St,name:Jon Doe,state:WA,houseNum:23619",
"ad1c:2dbf:fadf:2e87",
"E911AID:93a6:2db4:0589:261d,streetDir:NE,street:89th,zip:98052,city:Redmond,streetNameSuffix:St,name:John Doe,state:WA,houseNum:23619",
"E911AID:93a6:2db4:0589:261d,streetDir:NE,street:89th,zip:98052,city:Redmond,streetNameSuffix:St,name:JJ Doe,state:WA,houseNum:23619"
]
}
}
类定义
[Serializable]
public class locationResponseResponse
{
public locationResponse locationResponse { get; set; }
}
[Serializable]
public class locationResponse
{
public location[] locations { get; set; }
}
[Serializable]
public class location
{
public string E911AID { get; set; }
public string streetDir { get; set; }
public string street { get; set; }
public string zip { get; set; }
public string city { get; set; }
public string streetNameSuffix { get; set; }
public string name { get; set; }
public string state { get; set; }
public string houseNumber { get; set; }
}
反序列化代码段
public locationResponseResponse GetlocationResponseResponse(string jsonString)
{
locationResponseResponse _response = new locationResponseResponse();
if (!string.IsNullOrEmpty(jsonString))
{
JavaScriptSerializer ser = new JavaScriptSerializer();
_response = ser.Deserialize<locationResponseResponse>(jsonString);
}
return _response;
}
收到运行时错误
没有为BlackFlagAPI.locationResponse[]
类型定义无参数的构造函数。
json示例中的location
是字符串,而不是json对象。 因此,json解串器无法将它们转换为您的location
类。 字符串内容也不是序列化的json文档。
您需要为此注册一个自定义解析器,但是由于我从未使用过JavascriptSerializer
,因此无法告诉您如何操作。
另外,如果您可以控制json的来源,请使用json编码位置数据,而不是在字符串中嵌入自定义格式。
对于您发布的JSON,该类将类似于以下内容:
public class LocationResponse
{
public List<string> locations { get; set; }
}
public class Root
{
public LocationResponse locationResponse { get; set; }
}
然后,您需要手动解析位置。 否则,您需要更正JSON结构。
json中的Locations
不是json对象。 它们是格式为name:value
字符串,中间用,
分隔。
所以您需要分两步进行解析
1-反序列化json
var root = new JavaScriptSerializer().Deserialize<RootObject>(json);
2-解析每个字符串
var addrs = root.locationResponse.locations
.Select(x => x.Split(','))
.Select(parts =>
{
var l = new Location();
var props = l.GetType().GetProperties();
foreach (var part in parts)
{
var kv = part.Split(new char[] { ':' }, 2);
var prop = l.GetType().GetProperty(kv[0]);
if(prop != null)
prop.SetValue(l, kv[1]);
}
return l;
})
.ToList();
public class Location
{
public string E911AID { get; set; }
public string streetDir { get; set; }
public string street { get; set; }
public string zip { get; set; }
public string city { get; set; }
public string streetNameSuffix { get; set; }
public string name { get; set; }
public string state { get; set; }
public string houseNum { get; set; }
}
public class LocationResponse
{
public List<string> locations { get; set; }
}
public class RootObject
{
public LocationResponse locationResponse { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.