Paul
Paul

Reputation: 1235

JSON string deserialisation into C# object

I have the following result from an API call - what would be the best C# object structure to be able to deserialise the JSON string into the C# object?

The issue I have is that the JSON object are actually the name of the brand rather than

 "data":{"brands":{"brand":{"name:"Amazon"...etc 

the api is returning:

"data":{"brands":{"amazon":{"name:"Amazon",....

Which makes it difficult to create a brands--> brand hierarchy

See below for entire JSON string:

{
       "status":"success",
       "code":"1254",
       "message":"Hello",
       "data":{
          "brands":{
             "amazon":{
                "slug":"amazon",
                "name":"Amazon UK"
             },
             "boots":{
                "slug":"boots",
                "name":"boots UK"
             }
          }
       }
}

Upvotes: 0

Views: 85

Answers (1)

Pavel Anikhouski
Pavel Anikhouski

Reputation: 23208

The following structure should be fine, I guess

public class RootObject
{
    public string status { get; set; }
    public string code { get; set; }
    public string message { get; set; }
    public Data data { get; set; }
}

public class Data
{
    public Dictionary<string, Brand> brands { get; set; }
}
public class Brand
{
    public string slug { get; set; }
    public string name { get; set; }
}

And use ii like

var result = JsonConvert.DeserializeObject<RootObject>(yourJsonString);

Upvotes: 5

Related Questions