简体   繁体   English

如何将字符串格式的 json 数组转换为数组 []

[英]How can i convert json array in string format into array[]

I am rendering this data in a table, how can I convert Cuisine_style into array???我正在将这些数据呈现在表格中,如何将 Cuisine_style 转换为数组???

[
  {
    "Name": "Martine of Martine's Table",
    "City": "Amsterdam",
    "Cuisine_Style": "['French', 'Dutch', 'European']",
    "Ranking": 1,
    "Rating": 5,
    "Number of Reviews": 136
  },
  {
    "Name": "De Silveren Spiegel",
    "City": "Amsterdam",
    "Cuisine_Style": "['Dutch', 'European', 'Vegetarian Friendly', 'Gluten Free Options']",
    "Ranking": 2,
    "Rating": 4.5,
    "Number of Reviews": 812
  }
]

This is not an endorsement of bad object serialization这不是对坏 object 序列化的认可

If you absolutely must consume the data in this format, you can replace the single quotes with double quotes to make them JSON-compliant:如果您绝对必须使用这种格式的数据,您可以将单引号替换为双引号,以使其符合 JSON:

 const data = [ { "Name": "Martine of Martine's Table", "City": "Amsterdam", "Cuisine_Style": "['French', 'Dutch', 'European']", "Ranking": 1, "Rating": 5, "Number of Reviews": 136 }, { "Name": "De Silveren Spiegel", "City": "Amsterdam", "Cuisine_Style": "['Dutch', 'European', 'Vegetarian Friendly', 'Gluten Free Options']", "Ranking": 2, "Rating": 4.5, "Number of Reviews": 812 } ]; for (const item of data) item['Cuisine_Style'] = JSON.parse(item['Cuisine_Style'].replace(/'/g, '"')); console.log(data);

Try this.尝试这个。

 var arr_data = [{ "Name": "Martine of Martine's Table", "City": "Amsterdam", "Cuisine_Style": "['French', 'Dutch', 'European']", "Ranking": 1, "Rating": 5, "Number of Reviews": 136 }, { "Name": "De Silveren Spiegel", "City": "Amsterdam", "Cuisine_Style": "['Dutch', 'European', 'Vegetarian Friendly', 'Gluten Free Options']", "Ranking": 2, "Rating": 4.5, "Number of Reviews": 812 } ]; var y = JSON.stringify(arr_data); var x = y.toString(); console.log(x);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM