0

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.

2
  • 2
    Little confused on what you're asking for. Could you elaborate a bit? Commented Feb 20, 2013 at 18:44
  • sorry about that. i edited my question Commented Feb 20, 2013 at 18:51

1 Answer 1

2

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

2 Comments

Thanks. But then I try to get my products like this: 'User user = JsonConvert.DeserializeObject<User>(e.Result); List<Product> product = user.product;' but the user is null.
User user = JsonConvert.DeserializeObject<User>(e.Result[0]);

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.