CONVERTING JSON TO GRIDVIEW

Pooja Malohtra

Total Post:47

Points:331
Posted by  Pooja Malohtra
 4363  View(s)
Ratings:
Rate this:
 I set up a test DreamFactory server and I am trying to transform the results from JSON to a DataTable so I can display a GridView. I've tried several things with no success, this is my current code:

 var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                result = streamReader.ReadToEnd();
                dynamic d = JObject.Parse(result);
            }

            object obj = JsonConvert.DeserializeObject(result); 

            var table = JsonConvert.DeserializeObject<DataTable>(result); <--ERROR
            return table;
I've tried several methods but I am getting the following error most of the time:

Unexpected JSON token when reading DataTable. Expected StartArray, got StartObject. Path '', line 1, position 1.

It can deserialize fine as the object shows me the full deserialized JSON with no problems. Anything I can do?

EDIT: JSON Structure

{"record":[{"StatusID":1, "Identifier":"DQRJO1Q0IQRS",
 "Status":"WaitList",
"StatusDate":"2014-11-15",
"WorkedBy":"None",
"ContactedOn":"2014-11-15T14:21:55.623",
"Email":"email}]}
  1. Tom Cruser

    Post:28

    Points:196
    Re: Converting JSON to GridView

    Your json isn't a DataTable. All you need is to deserialize to

    var root = JsonConvert.DeserializeObject<RootObject>(result);
    public class Record
    {
        public int StatusID { get; set; }
        public string Identifier { get; set; }
        public string Status { get; set; }
        public string StatusDate { get; set; }
        public string WorkedBy { get; set; }
        public string ContactedOn { get; set; }
        public string Email { get; set; }
    }

    public class RootObject
    {
        public List<Record> Record { get; set; }
    }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!