微信小程序用console.log打印json数据

113 篇文章 1 订阅
12 篇文章 3 订阅
JSON.stringify(fun.nav_more(cp_nav_state))


JSON.parse()和JSON.stringify()

JSON.parse() 方法用于将一个 JSON 字符串转换为对象。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
</head>
<body>

<h2>从 JSON 字符串中创建一个对象</h2>
<p id="demo"></p>
<script>
var text = '{"employees":[' +
	'{"name":"菜鸟教程","site":"http://www.runoob.com" },' +
	'{"name":"Google","site":"http://www.Google.com" },' +
	'{"name":"Taobao","site":"http://www.taobao.com" }]}';
obj = JSON.parse(text);
document.getElementById("demo").innerHTML =
	obj.employees[1].name + " " + obj.employees[1].site;
</script>

</body>
</html>
注意:单引号写在{}外,每个属性名都必须用双引号,否则会抛出异常。




结果

从 JSON 字符串中创建一个对象

Google http://www.Google.com
详细:http://www.runoob.com/js/javascript-json-parse.html



JSON.stringify() 方法用于将 JavaScript 值转换为 JSON 字符串。

<script>
var str = {"name":"菜鸟教程", "site":"http://www.runoob.com"}
str_pretty1 = JSON.stringify(str)
document.write( "只有一个参数情况:" );
document.write( "<br>" );
document.write("<pre>" + str_pretty1 + "</pre>" );
document.write( "<br>" );
str_pretty2 = JSON.stringify(str, null, 4) //使用四个空格缩进
document.write( "使用参数情况:" );
document.write( "<br>" );
document.write("<pre>" + str_pretty2 + "</pre>" ); // pre 用于格式化输出
</script>
结果:
只有一个参数情况:
{"name":"菜鸟教程","site":"http://www.runoob.com"}

使用参数情况:
{
    "name": "菜鸟教程",
    "site": "http://www.runoob.com"
}
注意:日常用的时候直接  JSON.stringify(值)
详细:http://www.runoob.com/js/javascript-json-stringify.html





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

橙-极纪元JJY.Cheng

客官,1分钱也是爱,给个赏钱吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值