C#调用UPS地址验证API时JSON反序列化列表错误排查
错误信息
{"Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[OAuthWinForms2024.Objects.Candidate]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'XAVResponse.Candidate.AddressClassification', line 1, position 213."}
点击事件请求代码
private async void btnValidateAddress_Click(object sender, EventArgs e) { var serviceCollection = new ServiceCollection(); ConfigureServices(serviceCollection); var services = serviceCollection.BuildServiceProvider(); var _httpClientFactory = services.GetRequiredService<IHttpClientFactory>(); var client = _httpClientFactory.CreateClient(); var accesstoken = txtAccessToken.Text.ToString(); Candidate candidate = new Candidate(); Root addressVerification = new Root(); AddressKeyFormat addressKeyFormat = new AddressKeyFormat(); List<string> addressline = new List<string> { "1109 9th", "", "" }; addressKeyFormat.AddressLine = addressline; addressKeyFormat.PoliticalDivision1 = "AZ"; addressKeyFormat.PoliticalDivision2 = "Phoenix"; addressKeyFormat.PostcodePrimaryLow = "85007"; addressKeyFormat.PostcodeExtendedLow = ""; addressKeyFormat.Urbanization = ""; addressKeyFormat.CountryCode = "US"; candidate.AddressKeyFormat = addressKeyFormat; string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(candidate); var jsonStringV = @"{XAVRequest: " + jsonString + "}"; JObject myjson = JObject.Parse(jsonStringV); client.DefaultRequestHeaders.Add("Authorization", "Bearer " + accesstoken); var postData = new StringContent(myjson.ToString(), Encoding.UTF8, "application/json"); var Requestoption = "3"; var Version = "v1"; var request = await client.PostAsync("https://onlinetools.ups.com/api/addressvalidation/" + Version + "/" + Requestoption + "?regionalrequestindicator=string&maximumcandidatelistsize=1", postData); var response = await request.Content.ReadAsStringAsync(); // 错误发生在以下语句 Root addressVerification2 = JsonConvert.DeserializeObject<Root>(response); }
实体类定义
using System.Collections.Generic; namespace OAuthWinForms2024.Objects { // Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse); public class AddressClassification { public string Code { get; set; } public string Description { get; set; } } public class AddressKeyFormat { public string ConsigneeName { get; set; } public string AttentionName { get; set; } public List<string> AddressLine { get; set; } public string Region { get; set; } public string PoliticalDivision2 { get; set; } public string PoliticalDivision1 { get; set; } public string PostcodePrimaryLow { get; set; } public string PostcodeExtendedLow { get; set; } public string Urbanization { get; set; } public string CountryCode { get; set; } } public class Alert { public string Code { get; set; } public string Description { get; set; } } public class Candidate { public AddressClassification AddressClassification { get; set; } public AddressKeyFormat AddressKeyFormat { get; set; } } public class Response { public ResponseStatus ResponseStatus { get; set; } public List<Alert> Alert { get; set; } public TransactionReference TransactionReference { get; set; } } public class ResponseStatus { public string Code { get; set; } public string Description { get; set; } } public class Root { public XAVResponse XAVResponse { get; set; } } public class TransactionReference { public string CustomerContext { get; set; } } public class XAVResponse { public Response Response { get; set; } public string ValidAddressIndicator { get; set; } public string AmbiguousAddressIndicator { get; set; } public string NoCandidatesIndicator { get; set; } public AddressClassification AddressClassification { get; set; } public List<Candidate> Candidate { get; set; } } }
我认为自己已经正确使用了列表,但仍出现错误,希望得到解决方法。
内容的提问来源于stack exchange,提问作者SuperSavior

