通过 WEB API 请求返回的数据,data里的数据有 \ " 等等,这个格式怎么插入数据库呢?
想要通过这个接口,把下面data里的数据插入数据库:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/database/databaseAdd.html
// 这个是查询数据库返回的数据
{
"errcode": 0,
"errmsg": "ok",
"data": [
"{\"_id\":{\"sid\":{\"$numberDouble\":\"1003.0\"},\"mobile\":\"00000000000\"},\"totalCount\":{\"$numberInt\":\"12\"},\"created\":\"{\\\"$date\\\":{\\\"offset\\\":0}}\",\"createdMS\":{\"$numberLong\":\"1630921631001\"},\"createdStr\":\"2021-09-06 17:47:11\",\"createdDayStr\":\"2021-09-06\"}",
"{\"_id\":{\"sid\":{\"$numberDouble\":\"1003.0\"},\"mobile\":\"11111111111\"},\"totalCount\":{\"$numberInt\":\"62\"},\"created\":\"{\\\"$date\\\":{\\\"offset\\\":0}}\",\"createdMS\":{\"$numberLong\":\"1630921631001\"},\"createdStr\":\"2021-09-06 17:47:11\",\"createdDayStr\":\"2021-09-06\"}",
"{\"_id\":{\"sid\":{\"$numberDouble\":\"1003.0\"},\"mobile\":\"22222222222\"},\"totalCount\":{\"$numberInt\":\"63\"},\"created\":\"{\\\"$date\\\":{\\\"offset\\\":0}}\",\"createdMS\":{\"$numberLong\":\"1630921631001\"},\"createdStr\":\"2021-09-06 17:47:11\",\"createdDayStr\":\"2021-09-06\"}",
"{\"_id\":{\"sid\":{\"$numberDouble\":\"1004.0\"},\"mobile\":\"33333333333\"},\"totalCount\":{\"$numberInt\":\"34\"},\"created\":\"{\\\"$date\\\":{\\\"offset\\\":0}}\",\"createdMS\":{\"$numberLong\":\"1630921631001\"},\"createdStr\":\"2021-09-06 17:47:11\",\"createdDayStr\":\"2021-09-06\"}",
"{\"_id\":{\"sid\":{\"$numberDouble\":\"1005.0\"},\"mobile\":\"44444444444\"},\"totalCount\":{\"$numberInt\":\"84\"},\"created\":\"{\\\"$date\\\":{\\\"offset\\\":0}}\",\"createdMS\":{\"$numberLong\":\"1630921631001\"},\"createdStr\":\"2021-09-06 17:47:11\",\"createdDayStr\":\"2021-09-06\"}",
"{\"_id\":{\"sid\":{\"$numberDouble\":\"1007.0\"},\"mobile\":\"55555555555\"},\"totalCount\":{\"$numberInt\":\"28\"},\"created\":\"{\\\"$date\\\":{\\\"offset\\\":0}}\",\"createdMS\":{\"$numberLong\":\"1630921631001\"},\"createdStr\":\"2021-09-06 17:47:11\",\"createdDayStr\":\"2021-09-06\"}",
"{\"_id\":{\"sid\":{\"$numberDouble\":\"1006.0\"},\"mobile\":\"66666666666\"},\"totalCount\":{\"$numberInt\":\"30\"},\"created\":\"{\\\"$date\\\":{\\\"offset\\\":0}}\",\"createdMS\":{\"$numberLong\":\"1630921631001\"},\"createdStr\":\"2021-09-06 17:47:11\",\"createdDayStr\":\"2021-09-06\"}"
]
}
// 我想通过这段代码把上面的data里的数据加到数据库里
JSONArray data = res.getJSONArray("data");
// 发送请求 http api
String querystr = "db.collection('ordercollect').add({\n" +
" data: " + data + "\n" +
"})";
JSONObject body = new JSONObject();
body.put("env", env);
body.put("query", querystr);
文档里的data格式:
哥 你解决了吗
把转义符全部替换后,格式还是不对,插入不进去,报错:{"errcode":-605101,"errmsg":"SyntaxError: Line 2, column 14: Unexpected token 'identifier' TcbCode: [DATABASE_QUERY_PARSE_FAILED] rid: 61361370-26523dcb-079ce242"}
db.collection('ordercollect').add({ data: ["{"_id":{"sid":{"$numberDouble":"1007.0"},"mobile":"00000000000"},"totalCount":{"$numberInt":"284"},"created":"{"$date":{"offset":0}}","createdMS":{"$numberLong":"1630933871535"},"createdStr":"2021-09-06 21:11:11","createdDayStr":"2021-09-06"}"] })
斜杠是用来转义的,官方例子中不是也有么