一、JSON.parse(用于从一个字符串中解析出json 对象)ps:单引号写在{}外,每个属性都必须双引号,否则会抛出异常
let str = '[{"field":"thedate","v1":"20170102"},{"field":"rev_type","v1":"大数据收入"},{"field":"thismonth","v1":"201708"},{"field":"nextmonth","v1":"201709"},{"field":"depart_type","v1":"leader"},{"field":"admin_emp_id","v1":"role"}]';
JSON.parse(str);//str必须满足引号使用规范,即:双引号和单引号相互嵌套
[
{field: "thedate", v1: "20170102"},
{field: "rev_type", v1: "大数据收入"},
{field: "thismonth", v1: "201708"},
{field: "nextmonth", v1: "201709"},
{field: "depart_type", v1: "leader"},
{field: "admin_emp_id", v1: "role"}
]
二、JSON.stringify(用于从一个对象解析出字符串)
let arr=[
{field: "thedate", v1: "20170102"},
{field: "rev_type", v1: "大数据收入"},
{field: "thismonth", v1: "201708"},
{field: "nextmonth", v1: "201709"},
{field: "depart_type", v1: "leader"},
{field: "admin_emp_id", v1: "role"}
]
JSON.stringify(arr);//全是双引号
"[{"field":"thedate","v1":"20170102"},{"field":"rev_type","v1":"大数据收入"},{"field":"thismonth","v1":"201708"},{"field":"nextmonth","v1":"201709"},{"field":"depart_type","v1":"leader"},{"field":"admin_emp_id","v1":"role"}]"
三、qs.parse()将URL解析成对象的形式
本文介绍了JSON数据格式的解析与转换方法,包括JSON.parse和JSON.stringify的使用,并讲解了如何利用qs库进行URL参数的解析与生成。这些技能对于前端开发者来说非常实用。

990

被折叠的 条评论
为什么被折叠?



