JSON字符串转对象问题。

从接口返回了一段很奇葩的数据,想转换成数组使用,但是自己水平有限,尝试过JSON.parse和字符串的截取,但是最终未能解决该问题,希望诸位能帮忙解答此问题,谢谢!

返回的数据如下:

"[{"lng":121.580538,"lat":31.19932},{"lng":121.580919,"lat":31.199405},{"lng":121.58107,"lat":31.198751},{"lng":121.580683,"lat":31.198682}]"

期望的结果: 转换成数组

麻烦各位帮忙解决一下,谢谢!


BIG阳
浏览 423回答 1
1回答

万千封印

如果是返回的数据直接是这样的话, 你可以尝试把第一个双引号和最后一个双引号去掉, 然后再解析。<script>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var json_str = '"[{"lng":121.580538,"lat":31.19932},{"lng":121.580919,"lat":31.199405},{"lng":121.58107,"lat":31.198751},{"lng":121.580683,"lat":31.198682}]"';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 取出两边双引号&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; json_str = json_str.replace(/(^\")|(\"*$)/g, "");;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var json_obj = JSON.parse(json_str);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(json_obj);&nbsp; &nbsp; &nbsp; &nbsp; </script>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript