The first random exception like "Newtonsoft.Json.JsonReaderException" occurred while converting a partial json to a C # object

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); //just to check my json data. User searchResult = JsonConvert.DeserializeObject<User>(results.ToString()); //get exception on this line. searchResults.Add(searchResult); } 

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 ..

+6
source share
1 answer

In this line:

 User searchResult = JsonConvert.DeserializeObject<User>(result.ToString()) // result and not results 

you want to deserialize a simple result , not results .

Full 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); //just to check my json data. User searchResult = JsonConvert.DeserializeObject<User>(result.ToString()); //get exception on this line. searchResults.Add(searchResult); } 

By the way, you can replace the loop with some basic Linq:

 JObject response = JObject.Parse(jr); IList<User> searchRes = response["Results"].Select(r => JsonConvert.DeserializeObject<User>(r.ToString())).ToList(); 
+4
source

All Articles