1

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";
                }
            }
        }

    }
  • What is your error?As i understand you received null in detail object? – So_oP Oct 25 '19 at 15:37
  • Your JSON corresponds to a `List` not `List`. Deserialize it as such: `JsonConvert.DeserializeObject>(content);` See: [How to auto-generate a C# class file from a JSON string](https://stackoverflow.com/q/21611674/3744182). – dbc Oct 25 '19 at 15:37

1 Answers1

1

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);
mason
  • 31,774
  • 10
  • 77
  • 121
So_oP
  • 1,211
  • 15
  • 31