vatlayer api에서 반환 한 다음 JSON 객체가 있습니다.
{
"success":true,
"rates":{
"AT":{
"country_name":"Austria",
"standard_rate":20,
"reduced_rates":{
"foodstuffs":10,
"books":10,
"pharmaceuticals":10,
"passenger transport":10,
"newspapers":10,
"admission to cultural events":10,
"hotels":10,
"admission to entertainment events":10
}
},
"BE":{
"country_name":"Belgium",
"standard_rate":21,
"reduced_rates":{
"restaurants":12,
"foodstuffs":6,
"books":6,
"water":6,
"pharmaceuticals":6,
"medical":6,
"newspapers":6,
"hotels":6,
"admission to cultural events":6,
"admission to entertainment events":6
}
},
"BG":{
"country_name":"Bulgaria",
"standard_rate":20,
"reduced_rates":{
"hotels":9
}
}
...more obejcts
...more objects
...more objects
}
다음 클래스의 데이터를 읽고 싶습니다.
public class Country{
public string ShortCode{get;set;}// AT, BE, etc are examples of shortcode
public string Country_Name{get;set;}// Austria, Belgium etc
public decimal Standar_Rate{get;set;}// 20 and 21 respectively
}
문제는 웹 서비스가 JSON 개체의 배열로 데이터를 전송하지 않는다는 것입니다. 오히려 각 국가 단축 코드가 JSON의 키인 단일 객체를 전송합니다. 어떻게이 객체를 역 직렬화 할 List
또는 Array
의 Country
객체. 모든 JSON 변환기를 사용할 수 있습니다.
다음과 같이 응답을 모델링하십시오.
public class Response
{
public bool Success { get; set; }
public Dictionary<string, Country> Rates { get; set; }
}
그때:
var response = JsonConvert.DeserializeObject<Response>(json);
var allCountries = response.Rates.Values.ToList();
ShortCode
사전 키에있는는 제공되지 않습니다 . 다음을 사용하여 얻을 수 있습니다.
// Assuming the names have been fixed to be idiomatic...
var allCountries = response.Rates.Select(pair =>
new Country {
CountryName = pair.Value.CountryName,
StandardRate = pair.Value.StandardRate,
ShortCode = pair.Key
})
.ToList();
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다