无法提取JSON中的对象数组

时间:2019-07-11 11:49:36

标签: javascript json

无法从JSON数组对象数据中提取数据 我有此json错误格式的数据

    "ratings":"[{'id': 2, 'name': 'Confusing', 'count': 4}, {'id': 11, 
    'name': 'Longwinded', 'count': 8}, {'id': 26, 'name': 'Obnoxious', 
    'count': 1}, {'id': 8, 'name': 'Informative', 'count': 236}, {'id': 10, 
    'name': 'Inspiring', 'count': 123}, {'id': 22, 'name': 'Fascinating', 
    'count': 104}, {'id': 24, 'name': 'Persuasive', 'count': 73}, {'id': 1, 
    'name': 'Beautiful', 'count': 40}, {'id': 3, 'name': 'Courageous', 
    'count': 16}, {'id': 7, 'name': 'Funny', 'count': 18}, {'id': 25, 'name': 
    'OK', 'count': 29}, {'id': 9, 'name': 'Ingenious', 'count': 13}, {'id': 
    21, 'name': 'Unconvincing', 'count': 4}, {'id': 23, 'name': 'Jaw- 
    dropping', 'count': 8}]"

所以我用这个代替了双引号。

   var newJson = obj.replace(/([a-zA-Z0-9]+?):/g, '"$1":');
   newJson = newJson.replace(/'/g, '"');

结果就是这个。

    "ratings":"[{"id": 21, "name": "Unconvincing", "count": 21}, {"id": 8, 
    "name": "Informative", "count": 25}, {"id": 10, "name": "Inspiring", 
    "count": 26}, {"id": 25, "name": "OK", "count": 11}, {"id": 22, "name": 
    "Fascinating", "count": 9}, {"id": 9, "name": "Ingenious", "count": 14}, 
    {"id": 2, "name": "Confusing", "count": 3}, {"id": 26, "name": 
    "Obnoxious", "count": 4}, {"id": 1, "name": "Beautiful", "count": 12}, 
    {"id": 11, "name": "Longwinded", "count": 9}, {"id": 24, "name": 
    "Persuasive", "count": 5}, {"id": 23, "name": "Jaw-dropping", "count": 
    3}, {"id": 3, "name": "Courageous", "count": 0}, {"id": 7, "name": 
    "Funny", "count": 0}]"

所以我想删除两个双引号而不是所有双引号。 我希望数据格式看起来像这样。

    "ratings":[{"id": 21, "name": "Unconvincing", "count": 21}, {"id": 8, 
    "name": "Informative", "count": 25}, {"id": 10, "name": "Inspiring", 
    "count": 26}, {"id": 25, "name": "OK", "count": 11}, {"id": 22, "name": 
    "Fascinating", "count": 9}, {"id": 9, "name": "Ingenious", "count": 14}, 
    {"id": 2, "name": "Confusing", "count": 3}, {"id": 26, "name": 
    "Obnoxious", "count": 4}, {"id": 1, "name": "Beautiful", "count": 12}, 
    {"id": 11, "name": "Longwinded", "count": 9}, {"id": 24, "name": 
    "Persuasive", "count": 5}, {"id": 23, "name": "Jaw-dropping", "count": 
    3}, {"id": 3, "name": "Courageous", "count": 0}, {"id": 7, "name": 
    "Funny", "count": 0}] 

Can someone pls tell me how to remove those two double quotes. i try with many regex and can't get still desired result

1 个答案:

答案 0 :(得分:1)

您可以将输入的json括在大括号中,并在ratings键上执行替换。

 const json = {"ratings":"[{'id': 2, 'name': 'Confusing', 'count': 4}, {'id': 11, 'name': 'Longwinded', 'count': 8}, {'id': 26, 'name':'Obnoxious', 'count': 1}, {'id': 8, 'name': 'Informative', 'count': 236}, {'id': 10, 'name': 'Inspiring', 'count': 123}, {'id': 22, 'name': 'Fascinating', 'count': 104}, {'id': 24, 'name': 'Persuasive', 'count': 73}, {'id': 1, 'name': 'Beautiful', 'count': 40}, {'id': 3, 'name': 'Courageous', 'count': 16}, {'id': 7, 'name': 'Funny', 'count': 18}, {'id': 25, 'name': 'OK', 'count': 29}, {'id': 9, 'name': 'Ingenious', 'count': 13}, {'id': 21, 'name': 'Unconvincing', 'count': 4}, {'id': 23, 'name': 'Jaw- dropping', 'count': 8}]"}

 const newJSON = {ratings: json.ratings.replace(/'/g, '"')}

 const ratings = {ratings: JSON.parse(newJSON.ratings)}

 console.log(ratings)