So, here I have a JsonResult jr , it looks like this (the output is one line, I will format it here):
string jr = {"Results": [ {"Code":"DEMO", "Id":"1285", "Office":"9881", "Customers": [ 2713, 94204 ], "Account":196, "Appointments": [ 14, 58 ], "Role":0, "UserName":"demo", "UserId":3669, "FirstName":"Peter", "LastName":"Pan", "Phones": [ "(888) 888-8888" ], "Fax":null, "Email":" test@test.com ", "SMS":null, "RecordStatus":"1"}, {"Code":"DEMO", "Id":"9292", "Office":"9881", "Customers": [ 13, 904 ], "Account":196, "Appointments": [ 14, 58 ], "Role":0, "UserName":"berry", "UserId":302, "FirstName":"Jimmy", "LastName":"White", "Phones": [ "(888) 888-8888" ], "Email":" test@test.com ", "SMS":null, "RecordStatus":"1"} ], "TotalResults":2, "MilliSeconds":4}
Here is my User object:
public class User { public string FirstName { get; set; } public string LastName { get; set; } public string Id { get; set; } public string Office { get; set; } public string Email { get; set; } public string Code { get; set; } }
I am trying to use Deserializing Partial JSON Fragments to match json with my object: http://www.newtonsoft.com/json/help/html/SerializingJSONFragments.htm
I followed suit, but I got an error: The first exception of type Newtonsoft.Json.JsonReaderException occurred in Newtonsoft.Json.dll.
Many people on the internet say this is due to bad json. I checked, did not find anything bad. Below is my code:
JObject response = JObject.Parse(jr); IList<JToken> results = response["Results"].Children().ToList(); IList<User> searchResults = new List<User>(); foreach(JToken result in results) { System.Diagnostics.Debug.WriteLine(result);
The first output of result as follows:
{ "Code":"DEMO", "Id":"1285", "Office":"9881", "Customers": [ 2713, 94204 ], "Account":196, "Appointments": [ 14, 58 ], "Role":0, "UserName":"demo", "UserId":3669, "FirstName":"Peter", "LastName":"Pan", "Phones": [ "(888) 888-8888" ], "Fax":null, "Email":" test@test.com ", "SMS":null, "RecordStatus":"1" }
I donβt know why this exception occurs, wondering how to fix it ..