I have an API call using SmartyAddress, here is the result returned by the API call:
[ { "input_index": 0, "candidate_index": 0, "delivery_line_1": "xx", "last_line": "xx", "delivery_point_barcode": "xx", "components": { "primary_number": "xx", "street_name": "xx", "street_suffix": "xx", "city_name": "xx", "state_abbreviation": "xx", "zipcode": "xx", "plus4_code": "xx", "delivery_point": "xx", "delivery_point_check_digit": "xx" }, "metadata": { "record_type": "S", "zip_type": "Standard", "county_fips": "36047", "county_name": "Kings", "carrier_route": "C009", "congressional_district": "11", "rdi": "Residential", "elot_sequence": "0070", "elot_sort": "A", "latitude": 40.6223, "longitude": -74.00717, "precision": "Zip9", "time_zone": "Eastern", "utc_offset": -5, "dst": true }, "analysis": { "dpv_match_code": "Y", "dpv_footnotes": "AABB", "dpv_cmra": "N", "dpv_vacant": "N", "active": "Y" } } ]
Now I would like to use JSON to return this result, especially the analysis component, and here is the code I tried to write, but it always gives me an error: cannot deserialize the current json object to type 'system.collections.generic.list , and the following code:
public void Main() { try { var results = Client.Lookup(Dts.Variables["User::authID"].Value.ToString(), Dts.Variables["User::ServiceAddress"].Value.ToString(), Dts.Variables["User::ServiceCity"].Value.ToString(), Dts.Variables["User::ServiceState"].Value.ToString(), Dts.Variables["User::ServiceZipCode"].Value.ToString()); if (results == null) { throw new Exception("Failed to get DPV for ServiceAddress"); } else { var DPV = results.analysis; Dts.Variables["User::DPV"].Value = DPV; } } } catch (Exception ex) { Dts.Variables["User::DPV"].Value = "N"; throw ex; } Dts.TaskResult = (int)ScriptResults.Success; } public class Client { public static SmartyStreetsAddressLookup[] Lookup(string authId = null, string street = null, string city = null, string state = null, string zip = null) { try { using (WebClient web = new WebClient()) { JsonSerializer serial = new JsonSerializer(); string response = web.DownloadString(new Uri(String.Format(@"https://us-street.api.smartystreets.com/street-address?auth-id={0}&street={1}&city={2}&state={3}&zipcode={4}", authId, street, city, state, zip))); return JsonConvert.DeserializeObject<SmartyStreetsAddressLookup[]>(response); } } catch (Exception ex) { throw ex; } } } public class SmartyStreetsAddressLookup { public String[] metadata { get; set; } public String[] analysis { get; set; } }