3

I am using a thirdparty API and the Json response that I receive back is as below. Usually, using something like

Newtonsoft.Json.JsonConvert.DeserializeObject<MyModel>(response);

I can easily create a .Net model. However, I am struggling on how to formulate the below. I've tried KeyValuePairs, strings, modifying the actual Json but cannot seem to get any joy.

What am I missing?

{
  "1": [
    {
      "qty": 1,
      "discount": "flat",
      "price": 71.68
    }
  ],
  "2": [
    {
      "qty": 1,
      "discount": "flat",
      "price": 62.75
    }
  ],
  "3": [
    {
      "qty": 1,
      "discount": "flat",
      "price": 77.28
    }
  ],
  "4": [
    {
      "qty": 1,
      "discount": "flat",
      "price": 82.88
    }
  ],
  "5": [
    {
      "qty": 1,
      "discount": "flat",
      "price": 67.84
    }
  ]
}

Now, what is throwing me is that the numbers(1,2,3,4,5) are Identifiers so will not stay constant and could change each time you receive the response.

9
  • Try sticking the JSON into this tool to help you build the schema class. json2csharp.com Commented Jul 17, 2020 at 11:12
  • 2
    How about Dictionary<string, MyClass> ? (where MyClass is qty, discount, price) (or maybe Dictionary<string, MyClass[]> because although there is only 1 object per index, it appears to be an array). Commented Jul 17, 2020 at 11:12
  • 2
    @KieranDevlin It generates rubbish (class1, class2, class3 etc) Commented Jul 17, 2020 at 11:14
  • Your going to have to use a JObject and/or a custom serialiser for that JSON. I'd suggest your root cause here is whatever is generating that JSON. I'm presuming it's supposed to be an array but it looks like it's parsing the array index as an attribute, fix that not the parsing here Commented Jul 17, 2020 at 11:29
  • 2
    How does MyModel look like? It seems, that Dictionary<string, MyModel[]> (value should be an array) will help you Commented Jul 17, 2020 at 11:46

2 Answers 2

5

I think Newtonsoft can do this for you.

string json = @"{
  'Email': '[email protected]',
  'Active': true,
  'CreatedDate': '2013-01-20T00:00:00Z',
  'Roles': [
    'User',
    'Admin'
  ]
}";

var jsonReturn = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>( json );
Console.WriteLine( jsonReturn.Email );

Based on this link: https://www.newtonsoft.com/json/help/html/DeserializeObject.htm

This is the most basic explanation. Use a foreach to iterate over de nodes

Sign up to request clarification or add additional context in comments.

Comments

1

Using @Liam's suggestion of JObject I have come up with this working solution

public class MyObject
{
        public int id { get; set; }
        public int qty { get; set; }
        public string discount { get; set; }
        public decimal price { get; set; }
}

and then after retrieving the JSON response...

        var jsonObj = JsonConvert.DeserializeObject<JObject>(response);

        List<MyObject> myObjects = new List<MyObject>();

        foreach (var item in jsonObj.AsJEnumerable())
        {
                var csp = JsonConvert.DeserializeObject<List<MyObject>>(item.First.ToString());

                csp.ForEach(a => { a.product_id = Convert.ToInt32(item.Path); });
                    
                myObjects.AddRange(csp);
         }

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.