vue中string怎么转为json对象

vue中string转为json对象的方法:1、利用eval,语法为“result = eval('('jsonstr')');”;2、利用“JSON.parse()”,语法为“result = JSON.parse(jsonstr);”。

vue中string怎么转为json对象

本文操作环境:windows10系统、Vue2.9.6版,DELL G3电脑。

vue中string怎么转为json对象

Json字符串转换为json对象

1、使用eval

result = eval('(' + jsonstr + ')'); // jsonstr是json字符串

2、使用JSON.parse()

result = JSON.parse(jsonstr); // jsonstr是json字符串

eval和JSON.parse的区别:

  • eval 是javascript支持的方式,不需要严格的json格式的数据也可以转化

  • JSON.parse 是浏览器支持的转换方式,必须要标准的json格式才可以转换

【相关推荐:《vue.js教程》】

以上就是vue中string怎么转为json对象的详细内容,更多请关注www.sxiaw.com其它相关文章!