在c#中解析JSON -如何反序列化



我找到了一些例子,但它们不起作用。如果我能得到一个利率数组或以后解析成一个强类型的利率列表。

这就是我正在做的。

WebRequest webRequest = WebRequest.Create("https://api.exchangeratesapi.io/latest");
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
if (response.StatusDescription == "OK")
{
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
JToken rates = JValue.Parse(responseFromServer);
var ratechart = rates.Values();
}

下面是来自webrequest

的JSON响应结果
{"rates":{"CAD":1.516,"HKD":9.2695,"ISK":152.9,"PHP":58.048,"DKK":7.4363,"HUF":366.29,"CZK":26.303,"AUD":1.5565,"RON":4.8813,"SEK":10.1863,"IDR":17184.09,"INR":87.2305,"BRL":6.7979,"RUB":88.8807,"HRK":7.5745,"JPY":129.3,"THB":36.422,"CHF":1.1066,"SGD":1.6008,"PLN":4.5748,"BGN":1.9558,"TRY":8.9502,"CNY":7.7489,"NOK":10.211,"NZD":1.6737,"ZAR":18.2619,"USD":1.1938,"MXN":25.3204,"ILS":3.9606,"GBP":0.863,"KRW":1347.11,"MYR":4.8635},"base":"EUR","date":"2021-03-05"}

谢谢你的帮助

以下方法可能对您有所帮助!

HttpResponseMessage response = await client.PostAsync("https://api.exchangeratesapi.io/latest", new StringContent("", Encoding.UTF8, "application/json"));
if (response.IsSuccessStatusCode)
{
string jsonString = await response.Content.ReadAsStringAsync();
var settings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
MissingMemberHandling = MissingMemberHandling.Ignore
};                 

CLASS_AT_WHICH_JSON_STRING_IS_DESERIALIEZED rates = 
CLASS_AT_WHICH_JSON_STRING_IS_DESERIALIEZED();
rates =                 

JsonConvert.DeserializeObject        
<CLASS_AT_WHICH_JSON_STRING_IS_DESERIALIEZED>
(jsonString, settings);                       
}

相关内容

  • 没有找到相关文章

最新更新