0

I am attempting to deserialize a Json object that was returned from a web API with the structure as follows.

JsonResponse1

enter image description here

Here is my class structure in which the object is to be Deserialized into...

public class CandidateJson
    {
        public string response { get; set; }
        //public string result { get; set; }
        public Result result { get; set; }
        public string Candidates { get; set; }
        public List<row> rows { get; set; }
    }

    public class Result
    {
        public string result { get; set; }
        public string uri { get; set; }
    }

    public class row
    {
        public string no { get; set; }
        public List<FL> FL { get; set; }

    }

    public class FL
    {
        public string val { get; set; }
        public string content { get; set; }
    }

I am using the following line of code to Deserialized with no success....

var json = JsonConvert.DeserializeObject<CandidateJson>(JsonResult);

Upon execution of this line of code, I am prompted with the following error...

Unexpected character encountered while parsing value: {. Path 'response', line 1, position 13.

I would appreciate any assistance with this issue.

Please let me know if any additional information is needed.

Here is the raw JSON string:

{"response":{"result":{"Candidates":{"row":[
{"no":"1","FL":[{"val":"CANDIDATEID","content":"508304000012555617"},{"val":"Candidate ID","content":"ZR_129661_CAND"},{"val":"First Name","content":"PRODUCTION"},{"val":"Last Name","content":"TEST"},{"val":"Email","content":"patricia.conley@ampcorporate.com"},{"val":"Phone","content":"815-543-2109"},{"val":"Mobile","content":"815-555-5555"},{"val":"Street","content":"555 Test Ave"},{"val":"City","content":"DeKalb"},{"val":"State","content":"IL"},{"val":"Zip Code","content":"60115"},{"val":"SMCREATORID","content":"508304000000490014"},{"val":"Created By","content":"AMP Support IT Team"},{"val":"MODIFIEDBY","content":"508304000000227003"},{"val":"Modified By","content":"Nikki Bowman"},{"val":"Created Time","content":"2019-12-17 08:38:25"},{"val":"Updated On","content":"2019-12-20 15:23:10"},{"val":"Last Activity Time","content":"2019-12-20 15:23:10"},{"val":"SMOWNERID","content":"508304000000490014"},{"val":"Candidate Owner","content":"AMP Support IT Team"},{"val":"Source","content":"Non-Employee Referral"},{"val":"Email Opt Out","content":"false"},{"val":"Is Locked","content":"false"},{"val":"Is Unqualified","content":"false"},{"val":"Is Attachment Present","content":"false"},{"val":"Candidate Status","content":"Sales Training Scheduled"},{"val":"Career Page Invite Status","content":"0"},{"val":"Extension","content":"5555"},{"val":"Sales Training Date_ID","content":"508304000011808848"},{"val":"Sales Training Date","content":"2019-12-11 Digital Sales Training"},{"val":"Start Date","content":"2019-12-17"},{"val":"Candidate Job Category","content":"Print + Digital Outside"},{"val":"District Sales Manager","content":"Luke Wasowski"},{"val":"College Graduate","content":"false"},{"val":"Recruiter Initials","content":"NKB"},{"val":"Unit/Apt/Ste","content":"Apt 5"},{"val":"Hourly Rate","content":"5.00"},{"val":"Work State","content":"Illinois"},{"val":"Full Time/Part Time","content":"FTR"},{"val":"Work Email Address","content":"Nikki.Bowman@ampcorporate.com"},{"val":"EEO Class","content":"1.1"}]},
{"no":"2","FL":[{"val":"CANDIDATEID","content":"508304000011834365"},{"val":"Candidate ID","content":"ZR_125018_CAND"},{"val":"First Name","content":"Jennifer"},{"val":"Last Name","content":"Pedersen"},{"val":"Email","content":"jennyped248_hwo@indeedemail.com"},{"val":"Mobile","content":"+18157517187"},{"val":"City","content":"Genoa"},{"val":"State","content":"IL"},{"val":"Zip Code","content":"60135"},{"val":"Country","content":"United States"},{"val":"Experience in Years","content":"8"},{"val":"Current Employer","content":"WALMART"},{"val":"Current Job Title","content":"MOD TEAM MEMBER"},{"val":"Skill Set","content":"quick and exceptional customer experience, Helping and Advising Customers, Basic Word Processing, Communication Skills, Customer Service, Data Entry, Hard-Working, Intermediate Word Processing, Organisational Skills, Teamwork, Time Management, outstanding communication skills, Microsoft Word, Microsoft Excel, Microsoft Excel 2000, Microsoft Office, Microsoft Outlook, Microsoft PowerPoint, basic scheduling"},{"val":"SMCREATORID","content":"508304000000562001"},{"val":"Created By","content":"Matt Chenoweth"},{"val":"MODIFIEDBY","content":"508304000008810064"},{"val":"Modified By","content":"HR Department"},{"val":"Created Time","content":"2019-12-02 12:25:53"},{"val":"Updated On","content":"2019-12-12 09:04:51"},{"val":"Last Activity Time","content":"2019-12-12 09:04:51"},{"val":"SMOWNERID","content":"508304000000562001"},{"val":"Candidate Owner","content":"Matt Chenoweth"},{"val":"Source","content":"Indeed Resume"},{"val":"Email Opt Out","content":"false"},{"val":"Is Locked","content":"false"},{"val":"Is Unqualified","content":"false"},{"val":"Is Attachment Present","content":"true"},{"val":"Candidate Status","content":"Hired - AMP Office"},{"val":"Career Page Invite Status","content":"0"},{"val":"Source By","content":"Applied by Candidate"},{"val":"EMPID","content":"JFP147"},{"val":"Candidate Job Category","content":"Office - Digital Verification"},{"val":"College Graduate","content":"false"}]
}]}}
,"uri":"/recruit/private/json/Candidates/searchRecords"}}
MTL323
  • 177
  • 3
  • 14
  • 2
    Can you provide the actual json string? – Jamie Rees Jan 16 '20 at 18:34
  • 4
    http://json2csharp.com/ from [How to auto-generate a C# class file from a JSON string](https://stackoverflow.com/q/21611674/3744182) generates a data model that works, see https://dotnetfiddle.net/yZQVsF. https://app.quicktype.io?share=xYMGCLt5dpotj9FI99Uv should also work. – dbc Jan 16 '20 at 18:51
  • 1
    I'm inclined to close as a duplicate of [How to auto-generate a C# class file from a JSON string](https://stackoverflow.com/q/21611674/3744182), does that work for you? – dbc Jan 16 '20 at 18:55
  • Thank you! That class structure is what was needed to properly deserialize. – MTL323 Jan 16 '20 at 18:58
  • @dbc I personalty wouldn't since the question wasn't about "how to auto-generate a class". Post your comment in an answer (with maybe the content of the quicktype) and maybe a bold note to suggest using a tool like that, and this might help someone else? Just my 2 cents. – Tipx Jan 16 '20 at 19:01
  • 3
    _Edit-Paste Special-Paste JSON as classes_ in VS will also help. IMO, there are a lot of questions like that – Pavel Anikhouski Jan 16 '20 at 19:03
  • @PavelAnikhouski I feel dumb; I haven't been aware of that feature. Thanks! – Tipx Jan 16 '20 at 19:27

1 Answers1

0

I haven't tested it, but by the looks of it, your code should look like:

public class CandidateJson
{
    public Response response { get; set; }
}

public class Response
{
    public Result result { get; set; }
    public string uri { get; set; }
}

public class Result
{
    public Candidate Candidates { get; set; }
}

public class Candidate 
{
    public List<Row> row { get; set; }
}

public class Row
{
    public string no { get; set; }
    public List<FL> FL { get; set; }
}

public class FL
{
    public string val { get; set; }
    public string content { get; set; }
}

Note: You might want to use int or decimal instead of string for val and no, but there is not enough information for me to assert that.

Tipx
  • 7,367
  • 4
  • 37
  • 59