Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserialize JSON to C# Classes

Tags:

c#

json.net

Below is a (slightly) stripped down response I get from a REST API upon successful creation of a new "job code" entry. I need to deserialize the response into some classes, but I'm stumped.

For reference, I'm using JSON.NET in .NET 3.5 (running in a SSIS script in SQL Server 2008 R2) to attempt my deserialization. Here's the JSON - which I obviously have no control over as it's coming from someone else's API:

{    "results":{       "jobcodes":{          "1":{             "_status_code":200,             "_status_message":"Created",             "id":444444444,             "assigned_to_all":false,             "billable":true,             "active":true,             "type":"regular",             "name":"1234 Main Street - Jackson"          },          "2":{             "_status_code":200,             "_status_message":"Created",             "id":1234567890,             "assigned_to_all":false,             "billable":true,             "active":true,             "type":"regular",             "name":"4321 Some Other Street - Jackson"          }       }    } } 

In my C# code, I do have a "JobCode" class defined which only partially maps the JSON values to properties - I'm not interested in all of the data that's returned to me:

[JsonObject] class JobCode {     [JsonProperty("_status_code")]     public string StatusCode { get; set; }     [JsonProperty("_status_message")]     public string StatusMessage { get; set; }     [JsonProperty("id")]     public string Id {get; set;}     [JsonProperty("name")]     public string Name { get; set; }      //-------------------------------------------------------------------------------     // Empty constructor for JSON serialization support     //-------------------------------------------------------------------------------      public JobCode() { } } 

I'm attempting to deserialize the data via this call:

newResource = JsonConvert.DeserializeObject<JobCode>(jsonResponse); 

Where jsonResponse is the code outputted above.
When I execute the code, "newResource" always comes back as null - which is not unexpected because I know that there are actually multiple jobcodes in the data and this code is trying to deserialize it into a single JobCode object. I tried creating a new class called "JobCodes" that looks like this:

class JobCodes {     [JsonProperty("jobcodes")]     public List<JobCode>_JobCodes { get; set; } } 

And then I tried calling this:

newResource = JsonConvert.DeserializeObject<JobCodes>(jsonResponse); 

But the issue persists - my return object is null. What's throwing me off, I think, is the presence of the "1" and "2" identifiers. I don't know how to account for their presence in my object design and/or usage of the JSON.NET class / property attributes like [JsonObject],[JsonProperty], etc.

When I run the JSON data through JSON2CSharp, it constructs some weird-looking classes, so that hasn't proven too effective. I've validated the JSON with several different validators and it all checks out - I just don't know what I'm missing here.

Ultimately, I'd like to return a List from the JSON data, but I'm stumped on what I need to do to make that happen.

like image 594
Jake Bullet Avatar asked Jul 31 '14 06:07

Jake Bullet


People also ask

How do I deserialize a JSON file?

A common way to deserialize JSON is to first create a class with properties and fields that represent one or more of the JSON properties. Then, to deserialize from a string or a file, call the JsonSerializer. Deserialize method.

What is JSON deserialize C#?

Serialization and deserialization in . NET objects and vice versa is very common. Serialization is the process of converting . NET objects such as strings into a JSON format and deserialization is the process of converting JSON data into . NET objects.

What is JSON deserialize?

JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object). Regards, Daniel.

How do I deserialize dynamic JSON?

Dynamic; dynamic json = JsonSerializer. Deserialize<ExpandoObject>(jsonText); Console. WriteLine(json.name); This code prints out the string value of a name property that exists within the JSON text passed into the Deserialize method.


1 Answers

Your problem is twofold:

  1. You don't have a class defined at the root level. The class structure needs to match the entire JSON, you can't just deserialize from the middle.
  2. Whenever you have an object whose keys can change, you need to use a Dictionary<string, T>. A regular class won't work for that; neither will a List<T>.

Make your classes like this:

class RootObject {     [JsonProperty("results")]     public Results Results { get; set; } }  class Results {     [JsonProperty("jobcodes")]     public Dictionary<string, JobCode> JobCodes { get; set; } }  class JobCode {     [JsonProperty("_status_code")]     public string StatusCode { get; set; }     [JsonProperty("_status_message")]     public string StatusMessage { get; set; }     [JsonProperty("id")]     public string Id { get; set; }     [JsonProperty("name")]     public string Name { get; set; } } 

Then, deserialize like this:

RootObject obj = JsonConvert.DeserializeObject<RootObject>(json); 

Working demo here

like image 176
Brian Rogers Avatar answered Sep 20 '22 15:09

Brian Rogers