普通js通常这样转换
//这是一个json对象
var jsonobj = {"sztno":"330890811","transTime":"2018-5-7 08:00:36","amt":"50"};
//json对象转成json字符串
var jsonstr = JSON.stringify(jsonobj)
console.log("jsonstr=="+jsonstr)
//打印结果:"jsonstr=={"sztno":"330890811","transTime":"2018-5-7 08:00:36","amt":"50"}"
//由json字符串转成json对象
var obj1 = eval('(' + jsonstr + ')');
var obj2 = JSON.parse(jsonstr);
console.log("json obj1=="+JSON.stringify(obj1))
//打印结果:"obj1=={"sztno":"330890811","transTime":"2018-5-7 08:00:36","amt":"50"}"
console.log("json obj2=="+JSON.stringify(obj2))
//打印结果:"obj2=={"sztno":"330890811","transTime":"2018-5-7 08:00:36","amt":"50"}"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
微信小程序
//这是一个json对象
var jsonobj = {"sztno":"330890811","transTime":"2018-5-7 08:00:36","amt":"50"};
//json对象转成json字符串(同普通js中相同)
var jsonstr = JSON.stringify(jsonobj)
console.log("jsonstr=="+jsonstr)
//打印结果:"jsonstr=={"sztno":"330890811","transTime":"2018-5-7 08:00:36","amt":"50"}"
//由json字符串转成json对象,eval()函数在小程序用不了,被小程序禁用了
var obj1 = JSON.parse(jsonstr); //可用此方法来转换
console.log("json obj1=="+JSON.stringify(obj1))
//打印结果:"obj1=={"sztno":"330890811","transTime":"2018-5-7 08:00:36","amt":"50"}"
————————————————
版权声明:本文为CSDN博主「xiejunna」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/xiejunna/article/details/80226625
网友评论