0

I have an ASP MVC Web Api that returns these data as Json :

"[{\"OpID\":15,\"DeviceID\":1,\"DeviceType\":\"LED1\",\"DeviceState\":true,\"TurnOnTime\":\"2016-07-26T21:10:05.607\",\"TurnOffTime\":null,\"ToggleTime\":\"2016-07-26T21:10:05.61\",\"ToggleHour\":null},{\"OpID\":16,\"DeviceID\":5,\"DeviceType\":\"TV\",\"DeviceState\":true,\"TurnOnTime\":\"2016-07-26T21:10:09.283\",\"TurnOffTime\":null,\"ToggleTime\":\"2016-07-26T21:10:09.283\",\"ToggleHour\":null}]"

I`m trying to deserialize it using this code :

HttpClient client = new HttpClient();
                client.BaseAddress = new Uri("http://engeek.azurewebsites.net/");
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                HttpResponseMessage response = client.GetAsync("api/operation").Result;               
                string str = await response.Content.ReadAsStringAsync();                            
                List<operation> myDeserializedObjList = (List<operation>)JsonConvert.DeserializeObject(str , typeof(List<operation>));
                dataGridView1.DataSource = myDeserializedObjList;

and here is my model:

class operation
    {
        [JsonProperty("OpID")]
        public int OpID { get; set; }
        [JsonProperty("DeviceID")]
        public Nullable<int> DeviceID { get; set; }
        [JsonProperty("DeviceType")]
        public string DeviceType { get; set; }
        [JsonProperty("DeviceState")]
        public Nullable<bool> DeviceState { get; set; }
        [JsonProperty("TurnOnTime")]
        public Nullable<System.DateTime> TurnOnTime { get; set; }
        [JsonProperty("TurnOffTime")]
        public Nullable<System.DateTime> TurnOffTime { get; set; }
        [JsonProperty("ToggleTime")]
        public Nullable<System.DateTime> ToggleTime { get; set; }
        [JsonProperty("ToggleHour")]
        public Nullable<int> ToggleHour { get; set; }
    }

and the serialization code :

public string Getoperation()
        {
            var data = new List<operation>();
            data = db.operations.ToList();
            string str = JsonConvert.SerializeObject(data, Formatting.None);
            return str;
        }

It gives me : couldn`t convert or cast from string to List

What should I do ?

Jim Hewitt
  • 1,726
  • 4
  • 24
  • 26
Mohamad Maher
  • 33
  • 2
  • 6

1 Answers1

0

Somewhere in the MVC code where you are creating the JSON response, you are serializing the List<operation> as a JSON string, and then serializing that JSON string again.

Show the code where you serialize the List<operation> (that part looks good, thanks!) and the code where you return that JSON string to the client, and I'll probably be able to show you where that's happening.

You have a slight variation on the same problem this other guy had yesterday, except he only double-encoded one branch of his object, while you double-encoded the entire thing.

Community
  • 1
  • 1
  • public string Getoperation() { var data = new List(); data = db.operations.ToList(); string str = JsonConvert.SerializeObject(data, Formatting.None); return str; } – Mohamad Maher Jul 29 '16 at 17:36
  • @MohamadMaher, per your posted code and comment there shouldn't be any issue in deserializing. Somewhere you have done some mistake – Rahul Jul 29 '16 at 17:39