怎么给后端返回的json中添加数据

怎么给后端返回的json中添加数据,第1张

(1) 将如下代码copy之后保存为 server.js

(2) 然后执行: node server.js

var http = require('http')

var url = require('url')

// 访问的json地址与返回的json数据映射关系

var array=[

{

url:'/signup/index.json',

json:'{"tasks":[{"finishType":"人数优先","gmtEnd":"2015-11-19 11:30:00","gmtStart":"2015-11-17 11:30:00","id":98,"memo":"招新任务01","name":"招新任务01","requireNum":10,"signedupNum":0,"signupStatus":"","taskStatus":"进行中"}],"stat":"ok"}'

},

{

url:'/signup/applyCheck.json',

json:'{"signupInfo":{"alipayAccount":"20881021179902510156","alipayCardNo":"2088102117990251","birthday":"","certifyStatus":null,"city":"","college":"","email":"rjmuqiang@gmail.com","gender":null,"gmtCreate":null,"gmtModified":null,"id":2,"identityCardNo":"330283198903120025","identityCardPic":"","major":"","maxWeekHours":0,"minWeekHours":0,"mobile":"18905818799","province":"","qualificationPic":"","realName":"zhulu","recruitSource":"","signupTaskId":98,"status":null,"statusReason":"","student":false,"testScore":0,"trainScore":0},"checkResult":{"code":"SUCCESS","message":"处理成功","printResult":true,"success":true},"stat":"ok"}'

},

{

url:'/server/matchSuggest.json',

json:'{"TotalHits":88,"errorCode":0,"errorDesc":"no_error","MatchResults":[{"CatId":"4098","CatTitlePath":"%E5%86%85%E9%83%A8%E7%9F%A5%E8%AF%86%E5%BA%93%3E%E8%B4%A6%E6%88%B7%E5%9F%BA%E7%A1%80%E5%8F%8A%E8%B5%84%E4%BA%A7%E7%AE%A1%E7%90%86%EF%BC%88%E6%96%B0%EF%BC%89%3E%E8%B4%A6%E6%88%B7%E5%9F%BA%E7%A1%80%3E%E5%AF%86%E7%A0%81%3E%E6%89%8B%E5%8A%BF%E5%AF%86%E7%A0%81","ChannelNames":"","Content":"","CreatorName":"%E6%A1%83%E7%98%B4","GmtCreate":"2015-04-07 17:58:42","GmtModified":"2015-05-19 11:48:36","Id":"6056","Keywords":"","ModifierId":"12484","ModifierName":"%E7%89%A7%E6%9A%AE","Status":"PUBLISHED","Title":"%E9%80%9A%E8%BF%87%E6%94%AF%E4%BB%98%E5%AE%9D%E9%92%B1%E5%8C%85%EF%BC%8C%3Cfont+color%3Dred%3E%E5%BF%98%E8%AE%B0%3C%2Ffont%3E%E6%89%8B%E5%8A%BF%3Cfont+color%3Dred%3E%E5%AF%86%E7%A0%81%3C%2Ffont%3E%E7%9A%84%E5%A4%84%E7%90%86%E6%B5%81%E7%A8%8B","Type":"NORMAL","deleted":"N"}]}'

},

{

url:'/signup/signup.json',

json:'{"stat":"ok"}'

}

]

// var temResult='{"TotalHits":88,"errorCode":0,"errorDesc":"no_error","MatchResults":[{"CatId":"4098","CatTitlePath":"%E5%86%85%E9%83%A8%E7%9F%A5%E8%AF%86%E5%BA%93%3E%E8%B4%A6%E6%88%B7%E5%9F%BA%E7%A1%80%E5%8F%8A%E8%B5%84%E4%BA%A7%E7%AE%A1%E7%90%86%EF%BC%88%E6%96%B0%EF%BC%89%3E%E8%B4%A6%E6%88%B7%E5%9F%BA%E7%A1%80%3E%E5%AF%86%E7%A0%81%3E%E6%89%8B%E5%8A%BF%E5%AF%86%E7%A0%81","ChannelNames":"","Content":"%e7%87%95%e5%ad%90","CreatorName":"%E6%A1%83%E7%98%B4","GmtCreate":"2015-04-07 17:58:42","GmtModified":"2015-05-19 11:48:36","Id":"6056","Keywords":"","ModifierId":"12484","ModifierName":"%E7%89%A7%E6%9A%AE","Status":"PUBLISHED","Title":"%E9%80%9A%E8%BF%87%E6%94%AF%E4%BB%98%E5%AE%9D%E9%92%B1%E5%8C%85%EF%BC%8C%3Cfont+color%3Dred%3E%E5%BF%98%E8%AE%B0%3C%2Ffont%3E%E6%89%8B%E5%8A%BF%3Cfont+color%3Dred%3E%E5%AF%86%E7%A0%81%3C%2Ffont%3E%E7%9A%84%E5%A4%84%E7%90%86%E6%B5%81%E7%A8%8B","Type":"NORMAL","deleted":"N"},{"CatId":"4098","CatTitlePath":"%E5%86%85%E9%83%A8%E7%9F%A5%E8%AF%86%E5%BA%93%3E%E8%B4%A6%E6%88%B7%E5%9F%BA%E7%A1%80%E5%8F%8A%E8%B5%84%E4%BA%A7%E7%AE%A1%E7%90%86%EF%BC%88%E6%96%B0%EF%BC%89%3E%E8%B4%A6%E6%88%B7%E5%9F%BA%E7%A1%80%3E%E5%AF%86%E7%A0%81%3E%E6%89%8B%E5%8A%BF%E5%AF%86%E7%A0%81","ChannelNames":"","Content":"%e7%87%95%e7%aa%9d","CreatorName":"%E6%A1%83%E7%98%B4","GmtCreate":"2015-04-07 17:58:42","GmtModified":"2015-05-19 11:48:36","Id":"6056","Keywords":"","ModifierId":"12484","ModifierName":"%E7%89%A7%E6%9A%AE","Status":"PUBLISHED","Title":"%E9%80%9A%E8%BF%87%E6%94%AF%E4%BB%98%E5%AE%9D%E9%92%B1%E5%8C%85%EF%BC%8C%3Cfont+color%3Dred%3E%E5%BF%98%E8%AE%B0%3C%2Ffont%3E%E6%89%8B%E5%8A%BF%3Cfont+color%3Dred%3E%E5%AF%86%E7%A0%81%3C%2Ffont%3E%E7%9A%84%E5%A4%84%E7%90%86%E6%B5%81%E7%A8%8B","Type":"NORMAL","deleted":"N"}]}'

http.createServer(function(request, response){

response.writeHead(200,{

"Access-Control-Allow-Origin":"http://10.37.187.79:8000",

"Access-Control-Allow-Credentials": "true",

"Access-Control-Allow-Headers":"X-Requested-With",

"Access-Control-Allow-Methods":"PUT,POST,GET,DELETE,OPTIONS",

"X-Powered-By":"3.2.1",

"Content-Type":"application/jsoncharset=utf-8",

"Connection":"keep-alive"

})

var reqURL=request.url

var result=reqURL + " 对应的json结果数据是什么?请进行配置"

var i=array.length

while(i--){

if(reqURL.indexOf(array[i].url)==0){

result= array[i].json

}

console.log(111)

}

// var params = url.parse(request.url, true).query

// console.log(params)

response.write(result)

response.end()

}).listen(8787)

console.log('启动成功...')

我们知道,JSON是一种轻量级的数据交互的格式,大部分NO SQL数据库的存储都用JSON。MySQL从5.7开始支持JSON格式的数据存储,并且新增了很多JSON相关函数。MySQL 8.0 又带来了一个新的把JSON转换为TABLE的函数JSON_TABLE,实现了JSON到表的转换。

举例

我们看下简单的例子:

简单定义一个两级JSON 对象

mysql>set @ytt='{"name":[{"a":"ytt","b":"action"},  {"a":"dble","b":"shard"},{"a":"mysql","b":"oracle"}]}'Query OK, 0 rows affected (0.00 sec)

第一级:

mysql>select json_keys(@ytt)+-----------------+| json_keys(@ytt) |+-----------------+| ["name"]        |+-----------------+1 row in set (0.00 sec)

第二级:

mysql>select json_keys(@ytt,'$.name[0]')+-----------------------------+| json_keys(@ytt,'$.name[0]') |+-----------------------------+| ["a", "b"]                  |+-----------------------------+1 row in set (0.00 sec)

我们使用MySQL 8.0 的JSON_TABLE 来转换 @ytt。

mysql>select * from json_table(@ytt,'$.name[*]' columns (f1 varchar(10) path '$.a', f2 varchar(10) path '$.b')) as tt

+-------+--------+

| f1    | f2     |

+-------+--------+

| ytt   | action |

| dble  | shard  |

| mysql | oracle |

+-------+--------+

3 rows in set (0.00 sec)

举例二

再来一个复杂点的例子,用的是EXPLAIN 的JSON结果集。

JSON 串 @json_str1。

set @json_str1 = ' {  "query_block": {    "select_id": 1,    "cost_info": {      "query_cost": "1.00"    },    "table": {      "table_name": "bigtable",      "access_type": "const",      "possible_keys": [        "id"      ],      "key": "id",      "used_key_parts": [        "id"      ],      "key_length": "8",      "ref": [        "const"      ],      "rows_examined_per_scan": 1,      "rows_produced_per_join": 1,      "filtered": "100.00",      "cost_info": {        "read_cost": "0.00",        "eval_cost": "0.20",        "prefix_cost": "0.00",        "data_read_per_join": "176"      },      "used_columns": [        "id",        "log_time",        "str1",        "str2"      ]    }  }}'

第一级:

mysql>select json_keys(@json_str1) as 'first_object'+-----------------+| first_object    |+-----------------+| ["query_block"] |+-----------------+1 row in set (0.00 sec)

第二级:

mysql>select json_keys(@json_str1,'$.query_block') as 'second_object'+-------------------------------------+| second_object                       |+-------------------------------------+| ["table", "cost_info", "select_id"] |+-------------------------------------+1 row in set (0.00 sec)

第三级:

mysql> select json_keys(@json_str1,'$.query_block.table') as 'third_object'\G*************************** 1. row ***************************third_object: ["key","ref","filtered","cost_info","key_length","table_name","access_type","used_columns","possible_keys","used_key_parts","rows_examined_per_scan","rows_produced_per_join"]1 row in set (0.01 sec)

第四级:

mysql>select json_extract(@json_str1,'$.query_block.table.cost_info') as 'forth_object'\G*************************** 1. row ***************************forth_object: {"eval_cost":"0.20","read_cost":"0.00","prefix_cost":"0.00","data_read_per_join":"176"}1 row in set (0.00 sec)

那我们把这个JSON 串转换为表。

SELECT * FROM JSON_TABLE(@json_str1,

"$.query_block"

COLUMNS(

rowid FOR ORDINALITY,

NESTED PATH '$.table'

COLUMNS (

a1_1 varchar(100) PATH '$.key',

a1_2 varchar(100) PATH '$.ref[0]',

a1_3 varchar(100) PATH '$.filtered',

nested path '$.cost_info'

columns (

a2_1 varchar(100) PATH '$.eval_cost' ,

a2_2 varchar(100) PATH '$.read_cost',

a2_3 varchar(100) PATH '$.prefix_cost',

a2_4 varchar(100) PATH '$.data_read_per_join'

),

a3 varchar(100) PATH '$.key_length',

a4 varchar(100) PATH '$.table_name',

a5 varchar(100) PATH '$.access_type',

a6 varchar(100) PATH '$.used_key_parts[0]',

a7 varchar(100) PATH '$.rows_examined_per_scan',

a8 varchar(100) PATH '$.rows_produced_per_join',

a9 varchar(100) PATH '$.key'

),

NESTED PATH '$.cost_info'

columns (

b1_1 varchar(100) path '$.query_cost'

),

c INT path "$.select_id"

)

) AS tt

+-------+------+-------+--------+------+------+------+------+------+----------+-------+------+------+------+------+------+------+

| rowid | a1_1 | a1_2  | a1_3   | a2_1 | a2_2 | a2_3 | a2_4 | a3   | a4       | a5    | a6   | a7   | a8   | a9   | b1_1 | c    |

+-------+------+-------+--------+------+------+------+------+------+----------+-------+------+------+------+------+------+------+

|     1 | id   | const | 100.00 | 0.20 | 0.00 | 0.00 | 176  | 8    | bigtable | const | id   | 1    | 1    | id   | NULL |    1 |

|     1 | NULL | NULL  | NULL   | NULL | NULL | NULL | NULL | NULL | NULL     | NULL  | NULL | NULL | NULL | NULL | 1.00 |    1 |

+-------+------+-------+--------+------+------+------+------+------+----------+-------+------+------+------+------+------+------+

2 rows in set (0.00 sec)

当然,JSON_table 函数还有其他的用法,我这里不一一列举了,详细的参考手册。

请点击输入图片描述


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11934374.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存