John Okoroafor John Okoroafor - 4 days ago 5
C# Question

Deserializing json string to object using Newtonsoft

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.List
1[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.

Answer

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

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

Comments