Reputation: 13
I'm trying to convert a Json stream from API to a Object, i try also to convert in a List but the result it's the same.
screenshot of the problem https://prnt.sc/po3osq
this is the json [{"time":"1970-01-01T00:00:00.000Z","count":1}]
this the code
public class Mezzo
{
public DateTime time { get; set; }
public int count { get; set; }
}
public class GetBus
{
public List<Mezzo> Bus { get; set; }
}
public String GetListBus()
{
var addr = "http://192.168.43.131:3000/getBus";
var request = (HttpWebRequest)WebRequest.Create(addr);
request.Method = "GET";
var content = string.Empty;
using (var response = (HttpWebResponse)request.GetResponse())
{
using (var stream = response.GetResponseStream())
{
using (var sr = new StreamReader(stream))
{
content = sr.ReadToEnd();
var details = JsonConvert.DeserializeObject<List<GetBus>>(content);
Debug.WriteLine(details);
return "ciao";
}
}
}
}
Upvotes: 1
Views: 61
Reputation: 1293
Tested this already. It works.
string json = "[{\"time\":\"1970 - 01 - 01T00: 00:00.000Z\",\"count\":1}]";
var details = JsonConvert.DeserializeObject<List<Mezzo>>(json);
Console.WriteLine(details);
Upvotes: 1