呼喚你的編碼天才。我臉色發青,試圖弄清楚這一點。我正在呼叫一個公開暴露的 api 來回傳醫生資訊(https://npiregistry.cms.hhs.gov/api/?version=2.1&number=1225185168&pretty=on),我只是不知道如何達到較低的分支回傳我需要的資訊。它可能看起來很方形,但我不知道我錯過了什么。
提前致謝。
這是我的代碼(Core 6 中的控制臺應用程式):
using System.Net.Http.Headers;
namespace ConsoleProgram
{
public class NpiRegistryModel
{
public string? First_name { get; set; }
public string? Last_name { get; set; }
public string? Postal_code { get; set; }
public string? City { get; set; }
public int? Number { get; set; }
}
public class Program
{
private const string URL = "https://npiregistry.cms.hhs.gov/api/";
private static string urlParameters = "?version=2.1&number=1225185168";
private static void Main(string[] args)
{
HttpClient client = new()
{
BaseAddress = new Uri(URL)
};
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.GetAsync(urlParameters).Result;
if (response.IsSuccessStatusCode)
{
var dataObjects = response.Content.ReadAsAsync<IEnumerable<NpiRegistryModel>>().Result;
foreach (var d in dataObjects)
{
Console.WriteLine("Doctor: {0}, {1}", d.Last_name,d.First_name);
}
}
else
{
Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
}
client.Dispose();
}
}
}
這是我得到的錯誤:
System.AggregateException HResult=0x80131500 Message=一個或多個錯誤發生。(無法1[ConsoleProgram.NpiRegistryModel]' 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<T>) 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 'result_count', line 1, position 16.) Source=System.Private.CoreLib StackTrace: at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) at System.Threading.Tasks.Task在 System.Threading.Tasks.Task`1.get_Result() 將當前 JSON 物件(例如 {"name":"value"})反序列化為型別 'System.Collections.Generic.IEnumerable 1.GetResultCore(Boolean waitCompletionNotification)在 C:\Users\j####\source\Prototypes\TestApiCalls\HttpClientDemo\Program.cs:line 32 中的 ConsoleProgram.Program.Main(String[] args)
此例外最初是在此呼叫堆疊中引發的:[外部代碼]
內部例外 1:JsonSerializationException:無法將當前 JSON 物件(例如 {"name":"value"})反序列化為型別“System.Collections.Generic.IEnumerable`1[ConsoleProgram.NpiRegistryModel]”,因為該型別需要 JSON 陣列(例如 [1,2,3]) 以正確反序列化。要修復此錯誤,請將 JSON 更改為 JSON 陣列(例如 [1,2,3])或更改反序列化型別,使其成為普通的 .NET 型別(例如,不是像整數這樣的原始型別,而不是像這樣的集合型別可以從 JSON 物件反序列化的陣列或串列。JsonObjectAttribute 也可以添加到型別中以強制它從 JSON 物件反序列化。路徑“result_count”,第 1 行,位置 16。
uj5u.com熱心網友回復:
你必須首先決議你的json,之后你可以提取你需要的資料
using Newtonsoft.Json;
var json = response.Content.ReadAsStringAsync().Result;
var results = JObject.Parse(json)["results"][0];
NpiRegistryModel npiRegistryModel = results["basic"].ToObject<NpiRegistryModel>();
npiRegistryModel.PostalCode = (string) results["addresses"][0]["postal_code"];
npiRegistryModel.City = (string) results["addresses"][0]["city"];
npiRegistryModel.Number = (int) results["number"];
public partial class NpiRegistryModel
{
[JsonProperty("first_name")]
public string FirstName { get; set; }
[JsonProperty("last_name")]
public string LastName { get; set; }
[JsonProperty("postal_code")]
public string? PostalCode { get; set; }
[JsonProperty("city")]
public string? City { get; set; }
[JsonProperty("number")]
public int? Number { get; set; }
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/512379.html
上一篇:無法處理字串中的反斜杠或轉義字符
下一篇:嘗試按日期范圍對API集進行排序
