I am trying to read a JSON file from a URL. The JSON file has a header and some details that I need. Unfortunately, this is my first time doing this, so I would appreciate any guidance you can provide.
The error I am getting is a JSON parsing error. url:https://api.nobitex.ir/v2/trades/BTCUSDT
public class Trade
{
public object time { get; set; }
public string price { get; set; }
public string volume { get; set; }
public string type { get; set; }
}
public class Example
{
public string status { get; set; }
public IList<Trade> trades { get; set; }
}
/*******************************************************/
string url = "https://api.nobitex.ir/v2/trades/BTCUSDT";
using (var client = new HttpClient())
{
var response = await client.GetAsync(url);
if (response.StatusCode != HttpStatusCode.OK)
{
Console.WriteLine("err: " + response.StatusCode);
return;
}
string json = await response.Content.ReadAsStringAsync();
var st = JsonConvert.DeserializeObject<List<Example>>(json); ;
foreach (var t in st)
{
Console.WriteLine("status: " + t.status);
Console.WriteLine("trade: " + t.trades);
}
var det = JsonConvert.DeserializeObject<List<Trade>>(json); ;
foreach (var d in det)
{
Console.WriteLine("status: " + d.volume);
Console.WriteLine("status: " + d.type);
Console.WriteLine("status: " + d.time);
Console.WriteLine("status: " + d.price);
}
You're trying to deserialize into a collection:
JsonConvert.DeserializeObject<List<Example>>(json);
But the JSON to which you link isn't an array, it's an object:
{"status":"ok","trades":[{"time":1709824308663,"price":"66699.9","volume":"0.009622","type":"buy"},{"time":1709824290203,"price":"66458","volume":"0.000131","type":"sell"},{"time":1709824288176,"price":"66458","volume":"0.000617","type":"sell"},{"time":1709824288176,"price":"66460","volume":"0.000935","type":"sell"},{"time":1709824288176,"price":"66460.01","volume":"0.000001","type":"sell"},{"time":1709824288176,"price":"66460.01","volume":"0.000439","type":"sell"},{"time":1709824206312,"price":"66456.01","volume":"0.003139","type":"sell"},{"time":1709824199600,"price":"66748.97","volume":"0.001498","type":"buy"},{"time":1709824174337,"price":"66750","volume":"0.020422","type":"buy"},{"time":1709824174337,"price":"66700","volume":"0.001","type":"buy"},{"time":1709824165632,"price":"66456","volume":"0.000816","type":"sell"},{"time":1709824165632,"price":"66503","volume":"0.000133","type":"sell"},{"time":1709824165632,"price":"66551","volume":"0.0003","type":"sell"},{"time":1709824165632,"price":"66561.01","volume":"0.00044","type":"sell"},{"time":1709824165632,"price":"66600","volume":"0.000658","type":"sell"},{"time":1709824165632,"price":"66750","volume":"0.00837","type":"sell"},{"time":1709824152591,"price":"66750","volume":"0.019845","type":"sell"},{"time":1709824152591,"price":"66750","volume":"0.011308","type":"sell"},{"time":1709824152591,"price":"66765","volume":"0.000229","type":"sell"},{"time":1709824152591,"price":"66765","volume":"0.000403","type":"buy"}]}
This appears to be a single instance of Example
, so deserialize it to just that one instance:
JsonConvert.DeserializeObject<Example>(json);