问题描述
我试图消耗SmartyStreets JSON API LiveAddress和我有一些困难。我承认,我不是太熟悉JSON。不管怎么说,我已经尝试了几种不同的方法,我通常与错误落得无法反序列化JSON数组类型元数据。
I'm trying to consume SmartyStreets JSON LiveAddress API and I'm having some difficulties. I will admit that I'm not too familiar with JSON. Anyways, I've tried a few different methods and I usually end up with the error "Cannot deserialize JSON array into type Metadata".
这里是JSON字符串:
[{"input_index":0,"candidate_index":0,"delivery_line_1":"1600 Amphitheatre Pkwy","last_line":"Mountain View CA 94043-1351","delivery_point_barcode":"940431351000","components":{"primary_number":"1600","street_name":"Amphitheatre","street_suffix":"Pkwy","city_name":"Mountain View","state_abbreviation":"CA","zipcode":"94043","plus4_code":"1351","delivery_point":"00","delivery_point_check_digit":"0"},"metadata":{"record_type":"S","county_fips":"06085","county_name":"Santa Clara","carrier_route":"C058","congressional_district":"14"},"analysis":{"dpv_match_code":"Y","dpv_footnotes":"AABB","dpv_cmra":"N","dpv_vacant":"N","ews_match":false,"footnotes":"N#"}}]
我用 web应用程序创建类。
I used the jsontocsharp webapp to create classes.
下面是code我使用的是:
Here is the code I'm using:
using (var webClient = new WebClient())
{
var json = webClient.DownloadString("url");
var md = JsonConvert.DeserializeObject<Metadata>(json);
litTest.Text = md.county_name;
}
然后引发我上面提到的错误。
Which then throws the error I mentioned above.
任何援助将大大AP preciated。
Any assistance would be greatly appreciated.
感谢您,
安德鲁
Thank you,Andrew
推荐答案
我preFER使用动态
对象在这些情况下(无需创建丑类)
I prefer to use dynamic
object in these cases (No need for creating ugly classes)
如:
dynamic jsonObj = JsonUtils.JsonObject.GetDynamicJsonObject(json);
foreach(var item in jsonObj)
{
Console.WriteLine(item.delivery_line_1 + ", " +
item.last_line + ", " +
item.metadata.county_name + ", " +
item.components.street_name + ", " +
item.components.city_name );
}
下面是(只是复制和放大器;粘贴到您的项目)和的
Here is the source for Dynamic Json Object (just copy & paste to your project) and some samples
的 PS:这是更可读格式的JSON字符串的
[
{
"input_index": 0,
"candidate_index": 0,
"delivery_line_1": "1600 Amphitheatre Pkwy",
"last_line": "Mountain View CA 94043-1351",
"delivery_point_barcode": "940431351000",
"components": {
"primary_number": "1600",
"street_name": "Amphitheatre",
"street_suffix": "Pkwy",
"city_name": "Mountain View",
"state_abbreviation": "CA",
"zipcode": "94043",
"plus4_code": "1351",
"delivery_point": "00",
"delivery_point_check_digit": "0"
},
"metadata": {
"record_type": "S",
"county_fips": "06085",
"county_name": "Santa Clara",
"carrier_route": "C058",
"congressional_district": "14"
},
"analysis": {
"dpv_match_code": "Y",
"dpv_footnotes": "AABB",
"dpv_cmra": "N",
"dpv_vacant": "N",
"ews_match": false,
"footnotes": "N#"
}
}
]
这篇关于试图消耗SmartyStreets JSON与JSON.Net ...&QUOT;无法反序列化JSON数组类型的组件和QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!