尝试使用JSON.Net消费SmartyStreets JSON;无法将JSON数组反序列化为Components类型;

本文关键字:JSON 数组 反序列化 类型 Components Net 消费 SmartyStreets | 更新日期: 2023-09-27 18:26:25

我正在尝试使用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网络应用程序来创建类。

这是我正在使用的代码:

using (var webClient = new WebClient())
        {
            var json = webClient.DownloadString("url");
            var md = JsonConvert.DeserializeObject<Metadata>(json);
            litTest.Text = md.county_name;
        }

然后抛出我上面提到的错误。

如有任何协助,我们将不胜感激。

谢谢,Andrew

尝试使用JSON.Net消费SmartyStreets JSON;无法将JSON数组反序列化为Components类型;

我是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; }
    }
}

因此,最终它将取决于您是要使用静态类型的响应对象还是动态类型的响应。祝你好运

编辑:在样本响应中包含纬度/经度字段(最新发布)。

在这些情况下,我更喜欢使用dynamic对象(不需要创建难看的类)

例如:

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  );
}

以下是DynamicJson对象的来源(只需复制并粘贴到您的项目中)和一些样本

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#"
        }
    }
]