나는이 JSON 가지고 :가장 좋은 방법은 JSON에서 중첩 된 목록을 역 직렬화하는 - C#
당신이 볼 수 있듯이{
"CountriesAndCities": [
{
"CountryId": 2,
"CountryName": "Chile",
"CountryISOA3": "CHL",
"Cities": [
{
"CityId": 26,
"CityName": "Gran Santiago",
"Country": null
},
{
"CityId": 27,
"CityName": "Gran Concepción",
"Country": null
}
]
}
]
}
, 그것은 개체의 목록을, 그리고 그 객체는 또 다른리스트가 중첩 있습니다. 이 트릭을 수행, 지금
public class City
{
public int CityId { get; set; }
public string CityName { get; set; }
public Country Country { get; set; }
}
public class Country
{
public int CountryId { get; set; }
public string CountryName { get; set; }
public string CountryISOA3 { get; set; }
public ICollection<City> Cities { get; set; }
}
:
나는이 모델을 가지고
public ICollection<Country> Countries { get; set; }
public RegionViewModel()
{
// Pidiendo las ciudades al backend.
S3EWebApi webApi = new S3EWebApi();
HttpResponseMessage response = webApi.Get("/api/Benefits/GetCountriesAndCities");
string jsonResponseString = response.Content.ReadAsStringAsync().Result;
JObject jsonResponse = JsonConvert.DeserializeObject<JObject>(jsonResponseString);
string countriesAndCitiesJSon = jsonResponse["CountriesAndCities"].ToString();
Countries = JsonConvert.DeserializeObject<List<Country>>(countriesAndCitiesJSon);
}
하지만 나도 몰라, 난 그 우아한에서 너무 멀리 방법입니다 생각합니다. 더 나은 접근 방법이 있습니까? 감사합니다. . :)
나는 이것이 복제본이라고 확신합니다. 주위를 검색해보십시오. – visc