gpt4 book ai didi

c# - 使用 JSON 在 C# 中调用 API

转载 作者:太空狗 更新时间:2023-10-29 23:05:43 25 4
gpt4 key购买 nike

我有一个使用 SmartyAddress 的 API 调用,这是 API 调用返回的结果:

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

现在我想使用 JSON 返回这个结果,尤其是 analysis 组件,这是我尝试编写的代码,但它总是给我错误:cannot deserialize the当前的 json 对象类型为 'system.collections.generic.list 代码如下:

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

最佳答案

根据您的异常消息和您的代码,问题是您正试图将一个复杂对象从 json 反序列化为 2 个字符串数组,这两个数组显然不兼容。您应该使用与 json 中的内容相匹配的复杂类型。要抢先一步,您可以尝试 http://json2csharp.com/ ,输入您的 json,然后使用输出作为您的 c# 类结构的起点,然后匹配您的 json。

您当前在该站点中的 json 输出是这样的。

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 zip_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 string rdi { get; set; }
public string elot_sequence { get; set; }
public string elot_sort { get; set; }
public double latitude { get; set; }
public double longitude { get; set; }
public string precision { get; set; }
public string time_zone { get; set; }
public int utc_offset { get; set; }
public bool dst { 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 string active { get; set; }
}

public class RootObject
{
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; }
}

然后您可以将您的 json 反序列化为该结构。

return JsonConvert.DeserializeObject<RootObject[]>(response);

关于c# - 使用 JSON 在 C# 中调用 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40870380/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com