gpt4 book ai didi

javascript - 在 Javascript 中将字符串转换为对象

转载 作者:行者123 更新时间:2023-12-02 07:21:53 26 4
gpt4 key购买 nike

如何将字符串转换为对象?那是我的数据:

  "({"test1":[{"test2":55,"test":"15.06"},
{"test3":55,"test4":"15.08"}]})"

最佳答案

如果删除周围的括号,您将得到一个 JSON 字符串,可以使用 JSON.parse() 将其转换为对象:

var s = '({"test1":[{"test2":55,"test":"15.06"}, {"test3":55,"test4":"15.08"}]})',
j = s.replace(/^\((.+)\)$/, '$1'), //remove surrounding parentheses
o = JSON.parse(j);

console.log(o);

关于javascript - 在 Javascript 中将字符串转换为对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43727202/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com