有人能帮我从这个api反序列化JSON吗https://www.freeforexapi.com/api/live?pairs=EURUSD,GBPUSD到C#对象?我已经尝试了很多方法和例子,我在网上发现,非似乎正在工作
这里的速率是Map,所以响应C#类看起来像这样:
public class Response
{
public Dictionary<string, Rate> Rates{get;set;}
public int Code {get;set;}
}
public class Rate
{
public double Rate { get; set; }
public long TimeStamp { get; set; }
}
要反序列化:
var obj = JsonConvert.DeserializeObject<Response>(jsonObject);
例如,使用newtonsoft json为响应创建类
public class EURUSD {
public double rate { get; set; }
public int timestamp { get; set; }
}
public class GBPUSD {
public double rate { get; set; }
public int timestamp { get; set; }
}
public class Rates {
public EURUSD EURUSD { get; set; }
public GBPUSD GBPUSD { get; set; }
}
public class Root {
public Rates rates { get; set; }
public int code { get; set; }
}
然后反序列化响应,如
Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
您可以使用json2csharp网站轻松生成这样的类^