1

I have the following JSon string returned from a service:

{
    "products": {
        "product_1": [
            "this is description for product 1"
        ],
        "product_2": [
            "this is description for product 2"
        ]
    }
}

What I would like to do is convert it to a List of Product Class in c#. My Product class looks like this

public class Product
{
    public String Name{ get; set; }

    public String Description{ get; set; }
}

So for example above, "product_1" will be mapped to the Name property and "this is description for product 1" needs to be mapped to the Description property of the product class. Based on the Json, I would like to return a List.

Can anyone please help me out how can I achieve that using Newtonsoft.Json?

Thanks

magnolia
  • 33
  • 3
  • Wow, that format is "awesome". In this case, it would probably be simplest to use the LINQ or Enumerable features - see https://www.newtonsoft.com/json/help/html/QueryingLINQtoJSON.htm and https://www.newtonsoft.com/json/help/html/ParsingLINQtoJSON.htm – user2864740 Jul 21 '20 at 17:34
  • 1
    Does this answer your question? [How to convert Json array to list of objects in c#](https://stackoverflow.com/questions/19581820/how-to-convert-json-array-to-list-of-objects-in-c-sharp) – Hammas Jul 21 '20 at 17:36
  • Well not exactly.... the problem in my case is "product_1", "product_2" are all dynamic strings and product names so there is no way I can directly map them to property using JsonProperty attribute. Any suggestion? – magnolia Jul 21 '20 at 17:51

0 Answers0