Trying to consume SmartyStreets JSON with JSON.Net… “Cannot deserialize JSON array into type Components”

前端 未结 2 1923
醉梦人生
醉梦人生 2021-01-14 02:21

I\'m trying to consume SmartyStreets JSON LiveAddress API and I\'m having some difficulties. I will admit that I\'m not too familiar with JSON. Anyways, I\'ve tried a few di

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-14 02:26

    I prefer to use dynamic object in these cases (No need for creating ugly classes)

    such as:

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

    Here is the source for Dynamic Json Object (just copy & paste to your project) and some samples

    PS: This is your json string in more readable format

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

提交回复
热议问题