Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A first chance exception of type 'Newtonsoft.Json.JsonReaderException' occurred while convert partial json to c# object

Tags:

json

c#

So, here I have JsonResult jr, it looks like this(the output is one single line, I 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":"[email protected]",
       "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":"[email protected]",
       "SMS":null,
       "RecordStatus":"1"}
     ],
"TotalResults":2,
"MilliSeconds":4}

Here is my object User:

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'm trying to use Deserializing Partial JSON Fragments to map the json to my object: http://www.newtonsoft.com/json/help/html/SerializingJSONFragments.htm

I followed the example, but I got an error: A first chance exception of type 'Newtonsoft.Json.JsonReaderException' occured in Newtonsoft.Json.dll.

Many people online saying it's caused by bad json. I checked mine, didn't find anything wrong by myself. 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 result output looks like this:

{
       "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":"[email protected]",
       "SMS":null,
       "RecordStatus":"1"
}

Not sure why this exception happens, wondering how to fix it..

like image 682
Carrie Avatar asked Dec 22 '15 21:12

Carrie


1 Answers

In this line:

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

you want to deserialize a simple result and not the 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);
}

BTW, 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();
like image 81
Amir Popovich Avatar answered Oct 22 '22 18:10

Amir Popovich