0

I'm trying to do what I thought was a simple deserialization of a JSON. The code doesn't throw an error but just continues to run, which makes troubleshooting difficult. The end result I want is a datatable object with the contents of the JSON.

The code I have is this:

using Newtonsoft.Json;

HtmlElement body = webBrowser1.Document.GetElementsByTagName("body")[0];
string json = body.InnerHtml;
DataTable dt = (DataTable)JsonConvert.DeserializeObject(json, typeof(DataTable));

The contents of string json are as follows:

[
{
    "backColor": "White",
    "foreColor": "Black",
    "text": null,
    "title": null,
    "AC": {
        "backColor": "DarkRed",
        "foreColor": "White",
        "text": null,
        "title": "Planned Out of Service",
        "ac": "5DR",
        "fleet": "B757"
    }
},

{
    "backColor": "White",
    "foreColor": "Black",
    "text": null,
    "title": null,
    "AC": {
        "backColor": "Green",
        "foreColor": "White",
        "text": null,
        "title": "Unplanned Out of Service",
        "ac": "3CV",
        "fleet": "B737"
    }
}
]

I set a breakpoint right after the last line shown here and the code just runs forever.

EDIT: I see now that the problem is that my JSON contains objects inside. I created this code which accomplishes what I needed, posted here for reference. Thanks everyone for the comments.

using Newtonsoft.Json;
HtmlElement body = webBrowser1.Document.GetElementsByTagName("body")[0];
string json = body.InnerHtml;
DataTable dt = new DataTable();                          

List<RootObject> RO = JsonConvert.DeserializeObject<List<RootObject>>(json);

dt.Columns.Add("Nose", typeof(string));
dt.Columns.Add("Fleet", typeof(string));
// additional columns removed for clarity                               

for (int j = 0; j < RO.Count; j++)
{
    dt.Rows.Add();
    dt.Rows[j]["Nose"] = RO[j].AC.ac;
    dt.Rows[j]["Fleet"] = RO[j].AC.fleet;
    //additional rows removed for clarity                
}

public class RootObject
{
    public string backColor { get; set; }
    public string foreColor { get; set; }
    public string text { get; set; }
    public string title { get; set; }
    public AC AC { get; set; }
}

public class AC
{
    public string backColor { get; set; }
    public string foreColor { get; set; }
    public string text { get; set; }
    public string title { get; set; }
    public string ac { get; set; }
    public string fleet { get; set; }
}
masospaghetti
  • 348
  • 5
  • 12
  • 1
    Your code doesn't really make sense... Why would you expect this arbitrary JSON document to be deserializable to a `DataTable`? – Thomas Levesque Mar 09 '16 at 16:08
  • 1
    I think that the problem is the JSON structure, because that code only works with JSON that it only has pairs of key and value. [Here](http://stackoverflow.com/questions/11981282/convert-json-to-datatable) you can show an answer. – ganchito55 Mar 09 '16 at 16:08
  • Please see ["Should questions include “tags” in their titles?"](http://meta.stackexchange.com/questions/19190/should-questions-include-tags-in-their-titles), where the consensus is "no, they should not"! –  Mar 09 '16 at 16:13
  • 1
    OK, I just checked... apparently JSON *can* serialize and deserialize `DataTable`s, but it doesn't support object values (JSON does, but `DataTable` doesn't). So I suspect your code is actually throwing an exception on the call to `DeserializeObject`. – Thomas Levesque Mar 09 '16 at 16:14
  • Configure your debugger to break on exceptions, you will probably spot the problem. – Thomas Levesque Mar 09 '16 at 16:15

0 Answers0