[英]JSON convert array type to list in C#
I have some array data coming from an API in JSON format, and I want to convert an array type to a list.我有一些来自 JSON 格式的 API 的数组数据,我想将数组类型转换为列表。 There is an ASP.NET MVC project and I used the list in
Index
page.有一个 ASP.NET MVC 项目,我使用了
Index
页面中的列表。 How can I deserialize the array format into a list?如何将数组格式反序列化为列表?
Controller Controller
public async Task<IActionResult> ListCountries()
{
List<Country> countries = new List<Country>();
HttpClient _client = new HttpClient();
HttpResponseMessage _response = new HttpResponseMessage();
_client = _apiHelper.Initial();
_response = await _client.GetAsync("api/Countries/getall");
if (_response.IsSuccessStatusCode)
{
var results = _response.Content.ReadAsStringAsync().Result;
countries = JsonConvert.DeserializeObject<List<Country>>(results);
}
return View(countries);
}
Data数据
"data": [
{
"id": 1,
"countryName": "Afghanistan"
},
{
"id": 2,
"countryName": "Albania"
},
{
"id": 3,
"countryName": "Algeria"
},
Entity实体
public class Country
{
[Key]
public int Id { get; set; }
public string CountryName { get; set; }
}
Your json data is invalid format.您的 json 数据格式无效。 Json maybe have to be like this:
Json 可能必须是这样的:
{
"data": [ { "id": 1, "countryName": "Afghanistan" }, { "id": 2, "countryName": "Albania" }, { "id": 3, "countryName": "Algeria" } ] } “数据”:[{“id”:1,“countryName”:“阿富汗”},{“id”:2,“countryName”:“阿尔巴尼亚”},{“id”:3,“countryName”:“阿尔及利亚" } ] }
After that you should create 2 c# class like this:之后,您应该像这样创建 2 c# class :
public class JsonData
{
public List<Country> data { get; set; }
}
public class Country
{
public int id { get; set; }
public string countryName { get; set; }
}
Then you can deserialize it without any error.然后你可以反序列化它而不会出现任何错误。
public async Task<IActionResult> ListCountries()
{
List<Country> countries = new List<Country>();
HttpClient _client = new HttpClient();
HttpResponseMessage _response = new HttpResponseMessage();
_client = _apiHelper.Initial();
_response = await _client.GetAsync("api/Countries/getall");
if (_response.IsSuccessStatusCode)
{
var results = _response.Content.ReadAsStringAsync().Result;
countries = JsonConvert.DeserializeObject<JsonData>(results);
}
return View(countries);
}
The object has a variable data
that contains the countries whereas the class you're trying to serialize to does not. object 具有包含国家/地区的变量
data
,而您尝试序列化的 class 没有。
Having a class such as:有一个 class 例如:
public class Country{
[JsonProperty('data')]
public List<data> Details {get;set;}
public Country(){
Details = new List<data>();
}
public class data{
[Key]
[JsonProperty('id')]
public int Id { get; set; }
[JsonProperty('countryName')]
public string CountryName { get; set; }
}
and then to deserialize it you would need:然后反序列化它你需要:
countries = JsonConvert.DeserializeObject<Country>(results);
When it deserializes the object it will map data
to the Details
variable in the class Country
and map each value in the response array to the data
class When it deserializes the object it will map
data
to the Details
variable in the class Country
and map each value in the response array to the data
class
The structure shown is not a list/array of Countries but an object which has a property data which is a list/array of Contries:显示的结构不是国家列表/数组,而是一个 object,其属性数据是国家列表/数组:
public class Result
{
public List<Country> Data {get; set;}
}
...
var r = JsonConvert.DeserializeObject<Result>(results);
var countries = r.Data;
Minor note.小注。 Since you're using Json.NET it's OK that your properties don't match the case in json, but if you switch to System.Text.Json this would become an issue.
由于您使用的是 Json.NET,因此您的属性与 json 中的情况不匹配是可以的,但是如果您切换到 System.Text.Json,这将成为一个问题。
you have to parse and use JArray inside of your result您必须在结果中解析和使用 JArray
var countries = JObject.Parse(results)["data"].ToObject<List<Country>>();
// data for test
var results = @"{""data"": [
{
""id"": 1,
""countryName"": ""Afghanistan""
},
{
""id"": 2,
""countryName"": ""Albania""
},
{
""id"": 3,
""countryName"": ""Algeria""
}]}";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.