首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >试图使用SmartyStreets JSON和JSON.Net..。“无法将JSON数组反序列化为类型组件”

试图使用SmartyStreets JSON和JSON.Net..。“无法将JSON数组反序列化为类型组件”
EN

Stack Overflow用户
提问于 2012-03-01 18:43:33
回答 2查看 2.5K关注 0票数 4

我正在尝试使用SmartyStreets JSON LiveAddress API,并且遇到了一些困难。我承认我对JSON不太熟悉。无论如何,我尝试过几种不同的方法,并且通常会出现“不能将JSON数组反序列化为类型元数据”的错误。

这里是JSON字符串:

代码语言:javascript
复制
[{"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#"}}]

我使用詹托夏普 webapp应用程序创建类。

下面是我使用的代码:

代码语言:javascript
复制
using (var webClient = new WebClient())
        {
            var json = webClient.DownloadString("url");

            var md = JsonConvert.DeserializeObject<Metadata>(json);
            litTest.Text = md.county_name;
        }

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

如能提供任何协助,将不胜感激。

谢谢你,安德鲁

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-03-01 18:59:53

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

例如:

代码语言:javascript
复制
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  );

}

以下是动态Json对象 (只需将其复制和粘贴到您的项目)和一些样本的源代码

PS:这是您的json字符串,格式更易读。

代码语言:javascript
复制
[
    {
        "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#"
        }
    }
]
票数 4
EN

Stack Overflow用户

发布于 2012-03-02 16:44:40

我是圣烈士街的开发人员--谢谢你使用我们的服务!

您应该理解的主要一点是,JSON响应是一个地址对象数组,而不仅仅是一个。这是因为地址可能是模棱两可的,需要使用者进行选择/确认。

因此,这意味着您需要告诉Json.Net将JSON反序列化为顶级address对象,然后遍历每个地址以获取元数据(您试图直接解析元数据,因为返回的数组中每个地址都有一个元数据部分)。这基本上就是L.B在他的回答中所做的,只不过他增加了一些额外的开销,以便使用dynamic。

这里有一个替代解决方案,它使用与您的问题相同的"DeserializeObject“:

代码语言:javascript
复制
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; }
    }
}

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

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

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9522044

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档