将JSON数据从API转换为c#对象



我正在尝试将JSON数据从API转换为c#对象,我得到以下错误:

JSON值无法转换为countryesdemo . models . countrymodel。路径:$ | LineNumber: 0 | BytePositionInLine: 1

我的函数:

public static async Task getCountryData()
{
var apiURL = "https://restcountries.com/v3.1/alpha/tto";
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var jsonResults = await client.GetStringAsync(apiURL);
CountryModel? cshrpObj = System.Text.Json.JsonSerializer.Deserialize< CountryModel >(jsonResults);
Console.WriteLine(cshrpObj?.name);
}

c#类:

public class CountryModel
{
public string? name { get; set; }
public string tld { get; set; } = null!;
public string cca2 { get; set; } = null!;
public string ccn3 { get; set; } = null!;
public string cca3 { get; set; } = null!;
public string cioc { get; set; } = null!;
public string currency { get; set; } = null!;
public string idd { get; set; } = null!;
public string capital { get; set; } = null!;
public string altSpellings { get; set; } = null!;
public string region { get; set; } = null!;
public string subregion { get; set; } = null!;
public string languages { get; set; } = null!;
public string translations { get; set; } = null!;
public string latlng { get; set; } = null!;
public string demonym { get; set; } = null!;
public string landlocked { get; set; } = null!;
public string borders { get; set; } = null!;
public string area { get; set; } = null!;
}

API数据片段

[
{
"name": {
"common": "United States",
"official": "United States of America",
"nativeName": {
"eng": {
"official": "United States of America",
"common": "United States"
}
}
},
"tld": [
".us"
],
"cca2": "US",
"ccn3": "840",
"cca3": "USA",
"cioc": "USA",
"independent": true,
"status": "officially-assigned",
"unMember": true,
]

不幸的是你的json模型不正确。对于json,你需要这个模型

List<Country> myDeserializedClass = JsonSerializer.Deserialize<List<Country>>(jsonResults);
public class Country
{
public Name name { get; set; }
public List<string> tld { get; set; }
public string cca2 { get; set; }
public string ccn3 { get; set; }
public string cca3 { get; set; }
public string cioc { get; set; }
public bool independent { get; set; }
public string status { get; set; }
public bool unMember { get; set; }
}
public class Eng
{
public string official { get; set; }
public string common { get; set; }
}
public class Name
{
public string common { get; set; }
public string official { get; set; }
public NativeName nativeName { get; set; }
}
public class NativeName
{
public Eng eng { get; set; }
}

这是因为您引用的端点返回一个对象数组,而不是直接返回一个对象。

试试这个:

public static async Task getCountryData()
{
var apiURL = "https://restcountries.com/v3.1/alpha/tto";
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var jsonResults = await client.GetStringAsync(apiURL);
CountryModel[] cshrpObj = System.Text.Json.JsonSerializer.Deserialize<CountryModel[]>(jsonResults);
Console.WriteLine(cshrpObj.First().name);
}

最新更新