我正在尝试使用 SmartyStreets JSON LiveAddress API,但遇到了一些困难。我承认我对 JSON 不太熟悉。无论如何,我尝试了几种不同的方法,但通常以错误“无法将 JSON 数组反序列化为元数据类型”而告终。
这是 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#"}}]
我使用了 jsontocsharp webapp 来创建类。
这是我使用的代码:
using (var webClient = new WebClient())
{
var json = webClient.DownloadString("url");
var md = JsonConvert.DeserializeObject<Metadata>(json);
litTest.Text = md.county_name;
}
然后抛出我上面提到的错误。
如有任何帮助,我们将不胜感激。
谢谢, 安德鲁
最佳答案
我是 SmartyStreets 的一名开发人员——感谢您使用我们的服务!
您应该了解的主要一点是,JSON 响应是一组地址对象,而不仅仅是一个。这是因为地址可能不明确,需要消费者选择/确认。
所以这意味着你需要告诉 Json.Net 将 JSON 反序列化为顶级地址对象,然后遍历每个地址以获取元数据(你试图直接解析元数据,这是行不通的,因为返回的数组中的每个地址都有一个元数据部分)。这基本上就是 L.B 在他的回答中所做的,除了他添加了一些额外的开销以使用动态。
这是使用与您的问题相同的“DeserializeObject”的替代解决方案:
namespace JsonFun
{
using System;
using System.Net;
using Newtonsoft.Json;
class Program
{
private const string Url = "https://api.qualifiedaddress.com/street-address/?street=1600%20Amphitheatre%20Parkway&street2=&city=Mountain%20View&state=CA&zipcode=94043&candidates=10&auth-token=YOUR_AUTH_TOKEN_HERE";
static void Main()
{
using (var webClient = new WebClient())
{
var json = webClient.DownloadString(Url);
var candidate_addresses = JsonConvert.DeserializeObject<CandidateAddress[]>(json);
foreach (var item in candidate_addresses)
Console.WriteLine(item.metadata.county_name);
Console.ReadLine();
}
}
}
public class CandidateAddress
{
public int input_index { get; set; }
public int candidate_index { get; set; }
public string delivery_line_1 { get; set; }
public string last_line { get; set; }
public string delivery_point_barcode { get; set; }
public Components components { get; set; }
public Metadata metadata { get; set; }
public Analysis analysis { get; set; }
}
public class Components
{
public string primary_number { get; set; }
public string street_name { get; set; }
public string street_suffix { get; set; }
public string city_name { get; set; }
public string state_abbreviation { get; set; }
public string zipcode { get; set; }
public string plus4_code { get; set; }
public string delivery_point { get; set; }
public string delivery_point_check_digit { get; set; }
}
public class Metadata
{
public string record_type { get; set; }
public string county_fips { get; set; }
public string county_name { get; set; }
public string carrier_route { get; set; }
public string congressional_district { get; set; }
public double latitude { get; set; }
public double longitude { get; set; }
public string precision { get; set; }
}
public class Analysis
{
public string dpv_match_code { get; set; }
public string dpv_footnotes { get; set; }
public string dpv_cmra { get; set; }
public string dpv_vacant { get; set; }
public bool ews_match { get; set; }
public string footnotes { get; set; }
}
}
因此,最终取决于您是要使用静态类型的响应对象还是动态类型的响应对象。祝你好运!
编辑:样本响应中包含纬度/经度字段(新发布)。
关于c# - 尝试使用 JSON.Net 使用 SmartyStreets JSON... "Cannot deserialize JSON array into type Components",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9522044/