C #을 사용하여 JSON을 구문 분석하고 있습니다.
이 코드는 잘 작동합니다.
var json = webClient.DownloadString("API KEY");
Newtonsoft.Json.Linq.JObject o = Newtonsoft.Json.Linq.JObject.Parse(json);
Console.WriteLine(DefindexS);
price = (double)o["response"]["prices"]["5021"]["6"]["0"]["current"]["value"];
currency = (string)o["response"]["prices"]["5021"]["6"]["0"]["current"]["currency"];
Console.WriteLine("price" + price);
Console.WriteLine("Currency" + currency);
가격이 올바르게 인쇄됩니다.
여기에 캐치가 있습니다. 위의 두 경우 모두 "5021"은 사용자가 설정 한 변수로 대체해야합니다. JSON 데이터는 괜찮습니다. 숫자가 정확하면 적절한 값을 반환합니다.
변수는 DefindexS입니다. "5021"을 DefindexS로 대체하여 구문 분석을 시도했지만 (값을 5021로 설정했습니다) 처리되지 않은 예외 오류가 발생했습니다.
그런 다음 포맷을 시도하고 이렇게했습니다.
string realdef = String.Format("\"{0}\"", DefindexS.ToString());
Console.WriteLine(realdef);
var json = webClient.DownloadString("API KEY");
Newtonsoft.Json.Linq.JObject o = Newtonsoft.Json.Linq.JObject.Parse(json);
price = (double)o["response"]["prices"][realdef]["6"]["0"]["current"]["value"];
currency = (string)o["response"]["prices"][realdef]["6"]["0"]["current"]["currency"];
Console.WriteLine("price" + price);
Console.WriteLine("Currency" + currency);
결과:
"5021"과 충돌이 발생합니다. realdef는 "5021"로 인쇄하므로 형식이 제대로 이루어집니다. 여전히 오류가 발생하는 이유는 무엇입니까?
변수 주위에 따옴표를 추가 할 필요가 없습니다. 따라서이 코드 줄은 필요하지 않습니다.
string realdef = String.Format("\"{0}\"", DefindexS.ToString());
변경하면 작동합니다.
string realdef = DefindexS.ToString();
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다