我一直在尝试将json字符串转换为对象列表。这是Json字符串。
{"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"}]}
使用json2csharp我得到了这个类
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; }
}
但是当反序列化时,我得到了错误
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List
1 [Proxy.Models.Fooditem]'因为该类型需要JSON数组(例如[1,2,3])才能正确反序列化。
要修复此错误,请将JSON更改为JSON数组(例如[1,2,3])或更改反序列化类型,使其成为普通的.NET类型(例如,不是像整数这样的基本类型,而不是类似的集合类型可以从JSON对象反序列化的数组或List。 JsonObjectAttribute也可以添加到类型中以强制它从JSON对象反序列化。 `
这是我的反序列化代码。
var fooditems=JsonConvert.DeserializeObject<List<Fooditem>>(json)
对不起,这很长。非常感谢任何帮助。
答案 0 :(得分:1)
使用它进行反序列化:
var fooditems = JsonConvert.DeserializeObject<Rootobject>(jsonString);
你试图将RootObject json转换为FoodItems List是问题
答案 1 :(得分:1)
好的,我知道你的问题所在, 您的问题是您正在尝试将json字符串直接反序列化为列表,而json代码表明该列表应该是对象内的属性
json代码以{开头,这意味着它是一个有列表的对象。因此,为了使您的代码能够工作,json字符串应以[表示这是一个列表而不是具有列表的对象
开头]因此,要使代码正常工作,您必须选择:
要么告诉团队发送以[
使用此var fooditems = JsonConvert.DeserializeObject<Rootobject>(jsonString);
在这种情况下,它将返回RootObject类型的对象