ajt*_*tum 4 c# asp.net json json.net smartystreets
我正在尝试使用 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#"}}]
Run Code Online (Sandbox Code Playgroud)
我使用jsontocsharp webapp 来创建类。
这是我正在使用的代码:
using (var webClient = new WebClient())
{
var json = webClient.DownloadString("url");
var md = JsonConvert.DeserializeObject<Metadata>(json);
litTest.Text = md.county_name;
}
Run Code Online (Sandbox Code Playgroud)
然后抛出我上面提到的错误。
任何帮助将不胜感激。
谢谢你,安德鲁
我是 SmartyStreets 的开发人员——感谢您使用我们的服务!
您应该了解的主要内容是 JSON 响应是一组地址对象,而不仅仅是一个。这是因为地址可能不明确,需要消费者进行选择/确认。
所以这意味着您需要告诉 Json.Net 将 JSON 反序列化为顶级地址对象,然后遍历每个地址以获取元数据(您试图直接解析元数据,这不起作用,因为只有一个元数据返回的数组中每个地址的部分)。这基本上就是 LB 在他的回答中所做的,除了他为了使用动态增加了一些额外的开销。
这是使用与您的问题相同的“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; }
}
}
Run Code Online (Sandbox Code Playgroud)
因此,最终这将取决于您是要使用静态类型的响应对象还是动态类型的响应对象。祝你好运!
编辑:在示例响应中包含纬度/经度字段(新发布)。
| 归档时间: |
|
| 查看次数: |
2379 次 |
| 最近记录: |