1

I've been trying to convert a json string to a list of object. Here's the Json string.

{"fooditems":[{"item_id":1,"image_url":"http:\/\/tse1.mm.bing.net\/th?id=OIP.M201ca8470787c6ccf50f3b05118d2a27o0&pid=15.1","food_name":"Hollandia","food_description":"Maiores excepturi dolores recusandae quam. Et consequatur sunt et hic recusandae. Vero quisquam sed nemo cumque voluptates qui.","price":"144","item_special":"0","category":"2","date_created":"2016-10-18 07:12:45","date_updated":"2016-10-18 07:12:45","category_name":"Drinks"},{"item_id":2,"image_url":"http:\/\/tse1.mm.bing.net\/th?id=OIP.M201ca8470787c6ccf50f3b05118d2a27o0&pid=15.1","food_name":"Amstel Malta","food_description":"Delectus et voluptas qui. Quae dolorem rerum dignissimos et. Blanditiis porro temporibus perspiciatis voluptates similique.","price":"185","item_special":"1","category":"2","date_created":"2016-10-18 07:12:45","date_updated":"2016-10-18 07:12:45","category_name":"Drinks"},{"item_id":3,"image_url":"http:\/\/tse1.mm.bing.net\/th?id=OIP.M201ca8470787c6ccf50f3b05118d2a27o0&pid=15.1","food_name":"Egusi soup","food_description":"Aliquid et doloribus quis deserunt aut quas. Explicabo qui accusamus tempore omnis sint porro nostrum. Est nulla et commodi. Et reprehenderit rerum natus ipsa voluptas labore.","price":"152","item_special":"1","category":"1","date_created":"2016-10-18 07:12:45","date_updated":"2016-10-18 07:12:45","category_name":"Food"},{"item_id":4,"image_url":"http:\/\/tse1.mm.bing.net\/th?id=OIP.M201ca8470787c6ccf50f3b05118d2a27o0&pid=15.1","food_name":"Ponche","food_description":"Consequatur eius aut aut laudantium quis. Fuga facere a amet voluptatum. Ex optio accusamus corrupti porro repellendus ea.","price":"112","item_special":"1","category":"2","date_created":"2016-10-18 07:12:45","date_updated":"2016-10-18 07:12:45","category_name":"Drinks"},{"item_id":5,"image_url":"http:\/\/tse1.mm.bing.net\/th?id=OIP.M201ca8470787c6ccf50f3b05118d2a27o0&pid=15.1","food_name":"Teem","food_description":"Est atque eum et qui dolore ut. Dignissimos dicta qui nesciunt mollitia quo. Voluptatibus suscipit eaque laboriosam et ad. Iste illo vel consequatur doloribus.","price":"165","item_special":"1","category":"2","date_created":"2016-10-18 07:12:45","date_updated":"2016-10-18 07:12:45","category_name":"Drinks"},{"item_id":6,"image_url":"http:\/\/tse1.mm.bing.net\/th?id=OIP.M201ca8470787c6ccf50f3b05118d2a27o0&pid=15.1","food_name":"Yam Pepper soup","food_description":"Illo et ab doloribus adipisci quo hic aut. Ea fugiat ut sed minus ad non nesciunt modi. Voluptatibus nostrum labore cum maxime ullam.","price":"143","item_special":"1","category":"1","date_created":"2016-10-18 07:12:45","date_updated":"2016-10-18 07:12:45","category_name":"Food"},{"item_id":7,"image_url":"http:\/\/tse1.mm.bing.net\/th?id=OIP.M201ca8470787c6ccf50f3b05118d2a27o0&pid=15.1","food_name":"Coke","food_description":"Inventore nulla beatae incidunt delectus itaque provident accusantium. Aperiam veritatis doloribus veritatis illum rerum eum reprehenderit dolore. Ut quis fuga quia est.","price":"151","item_special":"0","category":"2","date_created":"2016-10-18 07:12:45","date_updated":"2016-10-18 07:12:45","category_name":"Drinks"},{"item_id":8,"image_url":"http:\/\/tse1.mm.bing.net\/th?id=OIP.M201ca8470787c6ccf50f3b05118d2a27o0&pid=15.1","food_name":"Jollof Rice","food_description":"Fugit labore vel ea sit voluptas consequuntur eum iste. Dolorum non distinctio sed explicabo. Saepe velit hic praesentium molestiae aperiam in autem. Soluta enim tenetur omnis tempore qui odit.","price":"172","item_special":"1","category":"1","date_created":"2016-10-18 07:12:45","date_updated":"2016-10-18 07:12:45","category_name":"Food"},{"item_id":9,"image_url":"http:\/\/tse1.mm.bing.net\/th?id=OIP.M201ca8470787c6ccf50f3b05118d2a27o0&pid=15.1","food_name":"Meat Pie","food_description":"Et vitae eveniet maxime maxime a. Explicabo consequatur quas atque non. Tempora voluptate similique dolore minus enim sit nulla eaque. Ut omnis aut incidunt assumenda excepturi.","price":"144","item_special":"0","category":"3","date_created":"2016-10-18 07:12:45","date_updated":"2016-10-18 07:12:45","category_name":"Snacks"},{"item_id":10,"image_url":"http:\/\/tse1.mm.bing.net\/th?id=OIP.M201ca8470787c6ccf50f3b05118d2a27o0&pid=15.1","food_name":"Doughnut","food_description":"Enim ipsa dolores velit autem. Illo rerum in voluptatem fugiat delectus. Sint quos provident soluta nisi ut. Cupiditate nemo temporibus qui fugiat earum fugit. Recusandae sit explicabo non exercitationem voluptatem fugiat.","price":"139","item_special":"1","category":"3","date_created":"2016-10-18 07:12:45","date_updated":"2016-10-18 07:12:45","category_name":"Snacks"},{"item_id":11,"image_url":"http:\/\/tse1.mm.bing.net\/th?id=OIP.M201ca8470787c6ccf50f3b05118d2a27o0&pid=15.1","food_name":"Fanta","food_description":"Voluptas qui asperiores qui ratione officia. Aut aliquid placeat sunt error saepe at. Repellat et ut sequi fuga aut ad debitis. Voluptatem minus sunt quam autem aut quod. Et sed dolorem qui quas.","price":"190","item_special":"1","category":"2","date_created":"2016-10-18 07:12:45","date_updated":"2016-10-18 07:12:45","category_name":"Drinks"},{"item_id":12,"image_url":"http:\/\/tse1.mm.bing.net\/th?id=OIP.M201ca8470787c6ccf50f3b05118d2a27o0&pid=15.1","food_name":"Viju Milk","food_description":"Minima excepturi voluptatem aut a sequi numquam deserunt veritatis. Enim ut vitae aut et. Non laudantium consequatur praesentium accusantium quo. Suscipit sequi dolor culpa consectetur quisquam dolorem.","price":"157","item_special":"0","category":"2","date_created":"2016-10-18 07:12:45","date_updated":"2016-10-18 07:12:45","category_name":"Drinks"},{"item_id":13,"image_url":"http:\/\/tse1.mm.bing.net\/th?id=OIP.M201ca8470787c6ccf50f3b05118d2a27o0&pid=15.1","food_name":"Nutri Milk","food_description":"Voluptatem atque consequatur aut est laudantium voluptas et. Earum et nihil minima ea ex. Et consequatur odio voluptas nihil quod.","price":"149","item_special":"1","category":"2","date_created":"2016-10-18 07:12:46","date_updated":"2016-10-18 07:12:46","category_name":"Drinks"},{"item_id":14,"image_url":"http:\/\/tse1.mm.bing.net\/th?id=OIP.M201ca8470787c6ccf50f3b05118d2a27o0&pid=15.1","food_name":"Eva Water","food_description":"Labore voluptatibus omnis omnis eaque. Occaecati aliquid voluptatum ducimus. Excepturi id magni quidem.","price":"137","item_special":"0","category":"2","date_created":"2016-10-18 07:12:46","date_updated":"2016-10-18 07:12:46","category_name":"Drinks"},{"item_id":15,"image_url":"http:\/\/tse1.mm.bing.net\/th?id=OIP.M201ca8470787c6ccf50f3b05118d2a27o0&pid=15.1","food_name":"Chapman","food_description":"Aut fugit recusandae laborum voluptas ut soluta accusantium. Impedit itaque nihil officia voluptas sed corrupti dicta. Reprehenderit id aut occaecati expedita doloribus et. Omnis quasi vel illum nam id tempora incidunt.","price":"194","item_special":"0","category":"2","date_created":"2016-10-18 07:12:46","date_updated":"2016-10-18 07:12:46","category_name":"Drinks"}]}

And using json2csharp I got this class

public class Rootobject
{
    public Fooditem[] Fooditems { get; set; }
}

public class Fooditem
{
    public int item_id { get; set; }
    public string image_url { get; set; }
    public string food_name { get; set; }
    public string food_description { get; set; }
    public string price { get; set; }
    public string item_special { get; set; }
    public string category { get; set; }
    public string date_created { get; set; }
    public string date_updated { get; set; }
    public string category_name { get; set; }
}

But when deserializing here's the error I get Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List1[Proxy.Models.Fooditem]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. `

Here's my deserializing code.

var fooditems=JsonConvert.DeserializeObject<List<Fooditem>>(json)

Sorry it's pretty long. Would appreciate any help.

9
  • 1
    Could you add your deserialization code? Commented Nov 30, 2016 at 9:55
  • I have not seen you deserialization code but i think you json code has problem with the image_url when it comes to escaping chars . Commented Nov 30, 2016 at 10:02
  • @BraveHeart I don't understand Commented Nov 30, 2016 at 14:40
  • An example of your data you have "http:\/\/tse1.mm.bing.net\/th?id=OIP.M201ca8470787c6ccf50f3b05118d2a27o0&pid=15.1" remove the backslash in your receiving urls . so it should be "http:/ /tse1.mm.bing.net/th?id=OIP.M201ca8470787c6ccf50f3b05118d2a27o0&pid=15.1" Commented Nov 30, 2016 at 14:43
  • note that I have added an extra space so it does not turn to a real link in the comment Commented Nov 30, 2016 at 14:45

2 Answers 2

1

use this for deserialization: var fooditems = JsonConvert.DeserializeObject<Rootobject>(jsonString);

you are trying to cast RootObject json to List of FoodItems is the problem

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

6 Comments

it would return you a root object and then you can access the property e.g. foodItems.FoodItems
I get a "unable to find constructor to use for type Fooditem.
fixed. I had to add a default constructor for my inner class Fooditem. Thanks
so you don't need to add a default constructor unless there is no non-default constructor.
The deserializer behind the scene creates an instance of the object you are trying to case and then parse the string and assign the values to instantiated object's properties. if there is a non-default constructor or a constructor with parameter and no default constructor .. the deserializer cannot decide what constructor to use with what parameters to pass hence you get this exception.
|
1

OK , I see where your problem is , Your problem is that you are trying to deserialize the json string directly to a list, while the json code indicates that the list should be a property inside an object

The json Code starts with { which means that it is an object that has a list . So for your code to work the json string should start with [ which says that this is a list and not an object that has a list

So to make your code works you have to options :

  1. Either you tell the team to send you the json string starting with [

  2. Use this var fooditems = JsonConvert.DeserializeObject<Rootobject>(jsonString);

in this case it will return an object of type RootObject

2 Comments

I think 2 is more plausible. So in the object of type RootObject, can I access the list inside???
Yes you can as it is a public property inside RootObject, but you know that you can name RootObject to anything else

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.