0

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"
             }
          }
       }
}
6
  • Which object model do you expect? Commented Apr 17, 2020 at 13:12
  • That sounds like your Data class (or whatever's representing the data field) just needs a Dictionary<string, Brand> property called Brands. Commented Apr 17, 2020 at 13:14
  • A Dictionary<string, Brand> is probably the best choice here Commented Apr 17, 2020 at 13:14
  • 1
    @PavelAnikhouski I was hoping to achieve something like RootObject-->Brands-->listOF Brand Commented Apr 17, 2020 at 13:15
  • @JonSkeet thanks that make sense Commented Apr 17, 2020 at 13:18

1 Answer 1

5

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.