简体   繁体   中英

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???

[
  {
    "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

If you absolutely must consume the data in this format, you can replace the single quotes with double quotes to make them JSON-compliant:

 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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