从URL C#

时间:2018-04-25 13:11:04

标签: c# json

我需要从URL加载JSON,而不知道原因(采用这种格式)。

在这个问题中,所有人都解决了:Dynamically load Json from URL on C#

但此时,我需要解析这个JSON:

{"Markets":[{"Label":"CHEESE/BTC","Name":"Cheese","Price":0.00000012,"Volume_24h":0.26702542,"Timestamp":1524662820}]}

JSON的格式不同。尝试过这样,但不起作用:

json = webClient.DownloadString("https://www.worldcoinindex.com/apiservice/ticker?key=0b6dmpsEyLlR7meh1QfALwEGE6dC3J&label=cheesebtc&fiat=btc");

dynamic obj = JsonConvert.DeserializeObject(json);

 if (obj.success == "true")
 {
     foreach (var result in obj.result)
     {
         if (result.Name == "Price")
             textbox1.text = result.value.ToString();
     }
 }

1 个答案:

答案 0 :(得分:2)

制作如下模型。并明确地将动态obj转换为该类。

public class Markets
{
    public string Label { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
    public decimal Volume_24h { get; set; }
    public string Timestamp { get; set; }
}


dynamic obj = JsonConvert.DeserializeObject<Markets>(json);

Markets obj = JsonConvert.DeserializeObject<Markets>(json);