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