Robin Wieruch
Robin Wieruch

Reputation: 15898

Deserialize array in array with json.NET in C#

I have an REST API which delivers in json format. The json looks like this

[{"user":{"name":"foo","url":"bar"}}
  ,[{"product":{"name":"banana","price":"85"}},
  {"product":{"name":"peach","price":"66"}},
  {"product":{"name":"strawberry","price":"78"}}]]

How can I transfer this with json.NET?

I tried some varients of

public class Product
{
    [JsonProperty("Name")]
    public string name { get; set; }

    [JsonProperty("price")]
    public string Price { get; set; }
}

public class RootObject
{
    public Product product { get; set; }
}

but it won't work with Product p = JsonConvert.DeserializeObject<Product>(e.Result);

How do I declare both classes? Sorry, but http://json2csharp.com/ didn't work.

Upvotes: 0

Views: 729

Answers (1)

user1964763
user1964763

Reputation: 46

you want to create a model in C# from JSON

public class User
{
   public string name { get; set; }
   public string url { get; set; }
   public List<Product> product { get; set; }

}

 public class Product
 {
    public string name { get; set; }
    public Decimal price { get; set; }
 }

Upvotes: 2

Related Questions