仿站 做网站,企业门户网站网站怎么获得,玉树电子商务网站建设公司,商水县住房城乡建设网站虽然把JSON数据存入mysql也是比较蛋疼#xff0c;但是相比使用Nodejs嵌套处理多个mysql查询并拼接返回数据也算是没mongo时的一个折中方案了。我使用python拼接了一个json格式的字符串#xff0c;却遇到了一些问题1#xff0c;如果把json数据转成str存入#xff0c;那么nod…虽然把JSON数据存入mysql也是比较蛋疼但是相比使用Nodejs嵌套处理多个mysql查询并拼接返回数据也算是没mongo时的一个折中方案了。我使用python拼接了一个json格式的字符串却遇到了一些问题1如果把json数据转成str存入那么nodejs获取数据的时候就无法使用json格式了处理方法就是importjsondata json.dumps(data_dict, ensure_asciiFalse)通过dumps就可以把python的字典转化成JSON转码后的JSON数据如下可以到http://www.bejson.com/ 去验证JSON格式是否正确{tongji1: [{sum_profit: 6032, counts: 15, win_counts: 8, span: 09:15:00}, {sum_profit: 0, counts: 0, win_counts: 0, span: 09:45:00}, {sum_profit: 1542, counts: 1, win_counts: 1, span: 10:15:00}, {sum_profit: 3084, counts: 2, win_counts: 2, span: 10:45:00}, {sum_profit: 1122, counts: 1, win_counts: 1, span: 11:15:00}, {sum_profit: 0, counts: 0, win_counts: 0, span: 13:15:00}, {sum_profit: -738, counts: 1, win_counts: 0, span: 13:45:00}, {sum_profit: 0, counts: 0, win_counts: 0, span: 14:15:00}, {sum_profit: 0, counts: 0, win_counts: 0, span: 14:45:00}], tongji2: [{sum_profit: 11042, counts: 20, win_counts: 12, span: 1}, {sum_profit: 0, counts: 0, win_counts: 0, span: 16}, {sum_profit: 0, counts: 0, win_counts: 0, span: 31}, {sum_profit: 0, counts: 0, win_counts: 0, span: 6}, {sum_profit: 0, counts: 0, win_counts: 0, span: 61}], tongji345: {avg_lose: 8, avg_win_span: 0, avg_win: 1907, avg_lose_span: 0, avg_max_lose_day: 389, avg_max_win_day: 813, avg_trade_counts: 1}}2MySQLdb插入数据时的一些注意事项sql insert into trades_info_f (id, data) values (%s, %s) % (id,data)如上代码使用 避免了JSON串的内部转义双引号这样就可以存入数据库了3在Nodejs端获取json数据exports.tongji (req, res) -openidreq.query.idsql SELECT data from trades_info_f WHERE id id console.log sqlmysqldb.query sql, (err, rows, fields)-console.log erriferrconsole.log rowsconsole.log rows[0].datadata JSON.parse(rows[0].data)return res.jsonp {status:0, message:ok, data:data}通过如下语句获取的数据rows需要进一步的处理如下的是原始数据[ { data: {tongji1: [{sum_profit: 42174, counts: 784, win_counts: 398, span: 09:15:00}, {sum_profit: 14647, counts: 757, win_counts: 377, span: 09:45:00}, {sum_profit: 51188, counts: 757, win_counts: 375, span: 10:15:00}, {sum_profit: 72475, counts: 771, win_counts: 409, span: 10:45:00}, {sum_profit: 4820, counts: 689, win_counts: 338, span: 11:15:00}, {sum_profit: 57657, counts: 691, win_counts: 346, span: 13:15:00}, {sum_profit: 73766, counts: 718, win_counts: 388, span: 13:45:00}, {sum_profit: 267, counts: 681, win_counts: 327, span: 14:15:00}, {sum_profit: 12207, counts: 582, win_counts: 303, span: 14:45:00}], tongji2: [{sum_profit: 469066, counts: 5528, win_counts: 2807, span: 1}, {sum_profit: -150245, counts: 142, win_counts: 45, span: 16}, {sum_profit: -51352, counts: 19, win_counts: 5, span: 31}, {sum_profit: -113061, counts: 1452, win_counts: 751, span: 6}, {sum_profit: -23535, counts: 107, win_counts: 48, span: 61}], tongji345: {avg_lose: 3592, avg_win_span: 4, avg_win: 625, avg_lose_span: 4, avg_max_lose_day: -2760, avg_max_win_day: 1977, avg_trade_counts: 41}} } ]rows[0].data就获得了所需的str数据之后使用JSON.parse()转换为JSON数据{tongji1: [{sum_profit: 42174, counts: 784, win_counts: 398, span: 09:15:00}, {sum_profit: 14647, counts: 757, win_counts: 377, span: 09:45:00}, {sum_profit: 51188, counts: 757, win_counts: 375, span: 10:15:00}, {sum_profit: 72475, counts: 771, win_counts: 409, span: 10:45:00}, {sum_profit: 4820, counts: 689, win_counts: 338, span: 11:15:00}, {sum_profit: 57657, counts: 691, win_counts: 346, span: 13:15:00}, {sum_profit: 73766, counts: 718, win_counts: 388, span: 13:45:00}, {sum_profit: 267, counts: 681, win_counts: 327, span: 14:15:00}, {sum_profit: 12207, counts: 582, win_counts: 303, span: 14:45:00}], tongji2: [{sum_profit: 469066, counts: 5528, win_counts: 2807, span: 1}, {sum_profit: -150245, counts: 142, win_counts: 45, span: 16}, {sum_profit: -51352, counts: 19, win_counts: 5, span: 31}, {sum_profit: -113061, counts: 1452, win_counts: 751, span: 6}, {sum_profit: -23535, counts: 107, win_counts: 48, span: 61}], tongji345: {avg_lose: 3592, avg_win_span: 4, avg_win: 625, avg_lose_span: 4, avg_max_lose_day: -2760, avg_max_win_day: 1977, avg_trade_counts: 41}}OK打开Postman格式正确了