2012-03-01 260 views
3

我試圖消費SmartyStreets JSON LiveAddress API,我有一些困難。我承認我對JSON不太熟悉。無論如何,我已經嘗試了幾種不同的方法,通常最終會出現錯誤「無法將JSON數組反序列化爲類型元數據」。試圖消費SmartyStreets JSON與JSON.Net ...「不能反序列化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#"}}] 

我使用的web應用程序jsontocsharp創建類。

這裏是我使用的代碼:

using (var webClient = new WebClient()) 
     { 
      var json = webClient.DownloadString("url"); 

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

然後引發我上面提到的錯誤。

任何援助將不勝感激。

謝謝 安德魯

回答

4

我更喜歡使用在這些情況下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 ); 

} 

下面是Dynamic Json Object源(只需複製&粘貼到您的項目)和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#" 
     } 
    } 
] 
+0

太棒了。我一定要更多地關注動態類。謝謝你指向我的動態Json對象和例子......你真的幫助我了! – ajtatum 2012-03-01 21:12:31

+0

不客氣。 – 2012-03-01 21:14:44

+0

@ L.B你是如何格式化的? – 2012-03-01 22:40:05

3

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

所以,最終這將取決於你是否想用一個靜態類型的響應對象或一個動態的工作。祝你好運!

編輯:包括樣本響應(新發布)中的緯度/經度字段。

+0

由於動態解釋問題,這似乎是更好的方法。我很欣賞這種見解。 – IAmTimCorey 2013-11-01 14:48:28