如何创建包含多个数组的 JSON 文件并向 dishes 添加列表
标准JSON转换说明
你原来编写的是类Dart风格的对象/Map代码,并非标准JSON。JSON是纯文本数据格式,不支持类定义、变量赋值等代码逻辑,核心语法规则如下:
- 所有对象键名、字符串值必须使用双引号包裹,不能用单引号
- 对象、数组的最后一个成员末尾禁止加多余逗号
- 字符串必须成对闭合,原代码中
Mutton Kadhai缺少末尾引号,需补全 - 原代码中的
&是HTML转义字符,JSON内直接写&即可 - 数字、布尔类型值不需要加引号,原有数值写法可以保留
- 不同对象可按需拥有不同字段,示例中部分酒店缺少菜单数组、部分酒店有优惠字段,均符合JSON规范
完整合法JSON文件
将内容保存为hotel_list.json即可:
{ "hotelData": [ { "index": 0, "restaurantName": "Monginis Cake Shop", "foodCategory": "The Cake Shop", "restReview": 5, "restImage": "assets/images/Restaurant1.png", "distanceFromLocation": 40, "restStatus": "open", "menuCategories": [ { "categoryName": "Pastry", "dishes": [ "Dutch Pastry", "Truffle Pastry", "Pipeapple Veg Pastry", "Brownee Sqare Veg Pastry", "Choco Lava Veg Cup" ] }, { "categoryName": "small cake ( half kg)", "dishes": [ "Sapphire Veg Small", "Deviles Delite Veg Small", "Duth Chocolate Heart Veg (Small)", "Shimmer Choco Veg Small" ] }, { "categoryName": "Breads", "dishes": [ "Sandwich Bread", "Multigrain Bread", "Brown Bread" ] }, { "categoryName": "Large cake", "dishes": [ "Alpine Choco Veg(Large)" ] }, { "categoryName": "Cookies", "dishes": [ "Almond Cocos Cookies", "Shewsburry Cookies", "Honey Crunch Cookis", "Kesar Cookies", "Jeeraa Cookies" ] }, { "categoryName": "Savouries", "dishes": [ "Panner pattice", "Veg Burger", "Pan Pizza Veg", "Veg pattice" ] }, { "categoryName": "Chocolate", "dishes": [ "Just Temptations" ] }, { "categoryName": "Plum & Bar cake", "dishes": [ "Fruit Bar cake" ] } ] }, { "index": 1, "restaurantName": "Swami Hotel", "foodCategory": "Family Hotel", "restReview": 2, "restImage": "assets/images/Restaurant2.png", "distanceFromLocation": 40, "restStatus": "open", "menuCategories": [ { "categoryName": "Veg Starter", "dishes": [ "Paneer pahadi kabab", "Garlic chana", "Masala papad", "Manchurian" ] }, { "categoryName": "Tandoori", "dishes": [ "Chicken Tandoori", "Chicken Tangadi Kabab", "Chicken Pahadi Kabab", "Mutton Kadhai" ] }, { "categoryName": "INDIAN BREAD", "dishes": [ "Aalu Paratha", "Paneer Paratha", "Nan", "Butter Nan", "Tandoori Roti", "Chapati" ] } ] }, { "index": 2, "restaurantName": "Diva Sagar", "foodCategory": "Family restaurant", "restReview": 5, "restImage": "assets/images/Restaurant3.png", "distanceFromLocation": 30, "restStatus": "open" }, { "index": 3, "restaurantName": "Smart open pick cake shop", "foodCategory": "Cake Shop", "restReview": 1, "restImage": "assets/images/Restaurant4.png", "distanceFromLocation": 10, "restStatus": "closed", "offer": "Get 5% discount" } ] }
向dishes数组添加数据的方法
现有结构中dishes是存储菜品名称的字符串数组,添加新条目的操作规则:
- 定位到目标酒店、目标分类对应的
dishes数组 - 在当前最后一个菜品末尾加逗号,换行写入双引号包裹的新菜品名即可,数组的最后一项末尾不能加逗号
示例:给第一个酒店的Pastry分类新增草莓酥点,修改后的片段如下:
{ "categoryName": "Pastry", "dishes": [ "Dutch Pastry", "Truffle Pastry", "Pipeapple Veg Pastry", "Brownee Sqare Veg Pastry", "Choco Lava Veg Cup", "Strawberry Pastry" ] }
如果需要为菜品附加价格、辣度、是否素食等属性,可以把dishes的数组项从字符串改为对象,实现结构化嵌套,示例:
"dishes": [ { "dishName": "Dutch Pastry", "price": 12, "isVeg": true }, { "dishName": "Truffle Pastry", "price": 15, "isVeg": true } ]
内容的提问来源于stack exchange,提问作者Mrunal
相关产品推荐
相关产品推荐

