C# 프로그램에 약간의 문제가 있습니다. C#의 첫 번째 프로그램입니다. 용서하십시오.C# 객체에 대한 JSON을 비 직렬화
내 json 파일을 개체로 deserialize하고 싶습니다. 그러나 클래스를 어떻게 작성해야할지 모르겠습니다. 뉴튼 소프트 JSON lib를 사용하여 임.
JSON 파일 :
http://api.nbp.pl/api/exchangerates/tables/A/last/?format=json
에서 Form1.cs :
private void button1_Click(object sender, EventArgs e)
{
CurrencyCodeValues k = new CurrencyCodeValues();
WebClient myWebClient = new WebClient();
dynamic result = myWebClient.DownloadString("http://api.nbp.pl/api/exchangerates/tables/A/last/?format=json");
IList<CurrencyCodeValues> m = JsonConvert.DeserializeObject<CurrencyCodeValues>(result);
}
으로 Class1.cs :
,class CurrencyCodeValues
{
public string table { get; set; }
public string no { get; set; }
public string effectiveDate { get; set; }
public List<rates_> rates { get; set; }
}
public class rates_
{
public string currency { get; set; }
public string code { get; set; }
public float mid { get; set; }
}
오류 메시지 :
현재 JSON 배열을 직렬화 할 수 없습니다 (예를 들어, [ '1,2,3])을'WindowsFormsApplication4.CurrencyCodeValues '유형으로 변환하려면 형식에 JSON 개체 (예 : { "name": "value"})가 필요하기 때문입니다. 이 오류를 수정하려면 JSON 객체를 JSON 객체 (예 : { "name": "value"})로 변경하거나 직렬화 해제 된 유형을 Array 또는 Collection 인터페이스 (예 : ICollection, IList)를 구현하는 유형으로 변경합니다. JSON 배열에서 역 직렬화 할 수 있습니다. JsonArrayAttribute를 유형에 추가하여 JSON 배열에서 역 직렬화하도록 할 수도 있습니다. 귀하의 오류 메시지로 경로 '', 1 호선, 위치 1.
그냥이 CurrencyCodeValues m = JsonConvert.DeserializeObject (결과를) 쓰기; –