当前位置: 首页 > Web前端 > HTML

json字符串与json对象的转换——学会了,你就立于不败之地!

时间:2023-04-02 18:33:33 HTML

1。jq插件使用parseJSON()$.parseJSON(jsonstr);//jq方法。将json字符串转换为json对象varstr1='{"name":"cxh","sex":"man"}';varobj=$.parseJSON(str1)console.log(obj);//{name:"cxh",sex:"man"}2、浏览器支持的转换方式(Firefox、chrome、opera、safari、ie)和其他浏览器:JSON.parse(jsonstr)//json字符串转json对象JSON。stringify(jsonobj);//json对象转json字符串varstr1='{"name":"cxh","sex":"man"}';varobj=JSON.parse(str1)console.log(obj);//{name:"cxh",sex:"man"}varobj2=JSON.stringify(obj2);console.log(obj);//'{"name":"cxh","sex":"man"}'3.js自带的eval函数,需要加括号eval('('+str+')');eval('('+jsonstr+')');//json字符串转json对象varstr1='{"name":"cxh","sex":"man"}';varobj=eval("("+str1+")")console.log(obj);//{name:"cxh",sex:"man"}小编不易,有收获的话,微信打赏小编一杯娃哈哈单身狗的葵花宝典,女生必备,敬请期待!