Although storing JSON data in MySQL is more of an egg ache, it is a compromise when compared to using nodejs nesting to handle multiple MySQL queries and stitching back data in a MONGO.
I used Python to stitch up a JSON-formatted string, but I encountered some problems
1, if the JSON data is transferred to STR, then Nodejs can not use the JSON format when retrieving the data.
The way to handle this is
Import= Json.dumps (data_dict, Ensure_ascii=false)
The Python dictionary can be converted to JSON by dumps
After transcoding the JSON data as follows, you can go to http://www.bejson.com/to verify that the JSON format is correct
{"Tongji1": [{"Sum_profit": 6032, "Counts": $, "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 ": 30 "Counts": 2, "win_counts": 2, "span": "10:45:00"}, {"Sum_profit": 1122, "counts": 1, "win_counts": 1, "span": "11:15:0 0 "}, {" 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": $, "win_counts": "Span": "1"}, { "Sum_profit": 0, "counts": 0, "win_counts": 0, "span": "+"}, {"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": " "}]," 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}}
2,mysqldb Some considerations when inserting data
""" INSERT into Trades_info_f (ID, data) VALUES ('%s ', '%s ') ""% (Id,data)
As the code above, "" "" "" "" "to avoid the internal escape of the JSON string double quotation marks, so that can be stored in the database
3, get JSON data on Nodejs side
Exports.tongji = (req, res) = req.query.id = "Select data from trades_info_f WHERE id = '" + ID + "'" console.log sql --if err console.log rows Console.log rows[0].data = json.parse (rows[0].data) return Res.jsonp {' status ': 0, ' message ': ' OK ', ' Data ':d ATA}
The data obtained by the following statement is required for further processing
The following is the original data
[{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": "1 0: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"}, {"s Um_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 ": 4 5, "span": "+"}, {"Sum_profit": -51352, "counts": +, "win_counts": 5, "span": "+"}, {"Sum_profit": -113061, "counts": 1 452, "win_counts": 751,"Span": "6"}, {"Sum_profit": -23535, "counts": 107, "win_counts":, "span": "" "}]," 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 the required STR data and then uses Json.parse () to convert to JSON 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_cou NTS ": 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, "spa N ":" 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": $, "span" : "+"}, {"Sum_profit": -51352, "counts": +, "win_counts": 5, "span": "+"}, {"Sum_profit": -113061, "counts": 1452, "win _counts ": 751," span ":" 6 "}, {"Sum_profit": -23535, "counts": 107, "win_counts":, "span": "" "}]," 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, open postman, the format is correct
Python stores json and nodejs out of MySQL