Home > DeveloperSection > Forums > Converting JSON to GridView
Pooja Malohtra
Pooja Malohtra

Total Post:47

Posted on    December-26-2014 10:12 PM

 .NET .NET  JSON  Gridview 

 1 Reply(s)
 2935  View(s)
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",

Tom Cruser
Tom Cruser

Total Post:28

Posted on    December-26-2014 10:32 PM

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

Don't want to miss updates? Please click the below button!

Follow MindStick