Barry D.
Barry D.

Reputation: 547

How Can I Deserialize JSON Data Using C#?

I have seen some other questions like this, but those are quite complex JSON data's that have objects within objects. Although the JSON I'm working with is never static, I doubt it's as complex as those. Also, it's my first time using JSON with C# so I'm a little clueless.

What I'm trying to achieve is to separate the data that is received from an API that I prompt using WebRequest in C#.

{
    "johhny.debt": {
         "id":35187540,
         "name":"johnny.debt",
         "profileIconId":786,
         "Level":30,
         "revisionDate":1428019045000
     }
 }

The returned JSON data is in a fashion like thereof.

I want to be able to access all of the properties of the above string in the following manner:

ID :
Name:
~~
~~
~~ ... and so forth.

I'm assuming some type of class has to be made for this?

All help is appreciated, thank you all in advance.

Upvotes: 0

Views: 80

Answers (2)

Coder1409
Coder1409

Reputation: 498

Just Create a class like this

  public class RootObject
{
    public int Id { get; set; }
    public string  name { get; set; }
    public int profileIconId { get; set; }
    public int Level { get; set; }
    public string revisionDate { get; set; }
}

then install json.Net and this code to your main method

var jsonObject=JsonConvert.DeserializeObject<RootObject>(jsonText);

That's all

Update

 var obj = JObject.Parse(json);
    var RootObject = new RootObject()
    {
        Id = (int)obj["johhny.debt"]["id"],
        Level = (int)obj["johhny.debt"]["Level"],
        name = (string)obj["johhny.debt"]["name"],
        profileIconId = (int)obj["johhny.debt"]["profileIconId"],
        revisionDate = (string)obj["johhny.debt"]["revisionDate"]
    };

Upvotes: 1

The Smallest
The Smallest

Reputation: 5783

Install Json.Net from Nuget

Install-Package Newtonsoft.Json

https://www.nuget.org/packages/Newtonsoft.Json/

Declare class for inner object ({"id":..., "name": ... }):

public class InnerObject
{
    [JsonProperty("id")]
    public int Id { get; set; }

    [JsonProperty("name")]
    public string  Username { get; set; }

    [JsonProperty("profileIconId")]
    public int ProfileIconId { get; set; }

    [JsonProperty("level")]
    public int Level { get; set; }

    [JsonProperty("revisionDate")]
    public string RevisionDate { get; set; }
}

As you can see you can specify rename mapping from json fields to .Net object properties using JsonPropertyAttribute.

Read your json to Dictionary<string,InnerObject> and get value of "johhny.debt" key:

var dict = JsonConvert.DeserializeObject<Dictionary<string, InnerObject>>(jsonText);
var johhny = dict["johhny.debt"];

Or if your need always to parse exact json property 'johhny.debt', you could create root object class:

public class RootObject
{
    [JsonProperty("johhny.debt")]
    public InnerObject JohhnyDept { get; set; }
}

And deserialize it:

var root = JsonConvert.DeserializeObject<RootObject>(jsonText);
var johhny = root.JohhnyDebt;

Upvotes: 1

Related Questions