我创建一个JSON对象并将其存储到mysql然后再次检索并解析它,当我对JSON对象进行字符串化时,它将属性置于“”之间,当我尝试解析检索到的字符串时,我得到意外的输入结束,这是代码
for(var i=0;i<objects.length;i++){
var o=new Object();
var id=objects[i].userData.id;
var name=objects[i].name;
o.id=id;
o.x=objects[i].position.x;
o.y=objects[i].position.y;
o.z=objects[i].position.z;
o.r=objects[i].rotation.z;
resources[name]=o;
}
我将JSON.stringify(resources)
插入mysql,插入的字符串如{"animalsherd0":{"id":"11","x":"4.7","y":"19.6","z":"18.8","r":0},"oasis1":{"id":"19","x":"-11.3","y":"19.6","z":"18.8","r":0},"corn2":{"id":"24","x":"-5.6","y":"19.6","z":"5.0","r":0}}
检索到的字符串存储在变量中,那我该如何解析这个字符串呢?
答案 0 :(得分:0)
试试这个: jQuery.parseJSON
var obj = jQuery.parseJSON('{"animalsherd0":{"id":"11","x":"4.7","y":"19.6","z":"18.8","r":0},"oasis1":{"id":"19","x":"-11.3","y":"19.6","z":"18.8","r":0},"corn2":{"id":"24","x":"-5.6","y":"19.6","z":"5.0","r":0}}');
console.log(obj);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>