我的表架构是这样的,
const hash = {};
for(const value of data) {
const group = hash[value.custType] || (hash[value.custType] = {});
const group2 = group[value.code] || (group[value.code] = []);
group2.push(value);
}
const result = Object.values(hash).map(Object.values);
最初,我的过程返回的数据为
DECLARE @mytable AS TABLE(ID INT, EmpName VARCHAR(10), Age INT);
INSERT INTO @mytable
VALUES
(1, 'a', 1),
(2, 'b', 2),
(3, 'c', 3),
(4, 'd', 4),
(5, 'e', 5),
(6, 'f', 6),
(7, 'g', 7),
(8, 'h', 8),
(9, 'i', 9),
(10, 'j', 10);
处理完数据后,将网页发送为
SELECT ID, EmpName, Age FROM @mytable;
如果我使用
[{1,"a",1},
{2,"b",2},
{3,"c",3},
{4,"d",4},
{5,"e",5},
{6,"f",6},
{7,"g",7},
{8,"h",8},
{9,"i",9},
{10,"j",10}]
结果将类似于
SELECT ID, EmpName, Age
FROM @mytable
FOR JSON AUTO;
因此,我需要得到的是相同的响应,但没有列名,因为前端库使用不带键的JSON数组。 我希望我最终会清楚。谢谢。
答案 0 :(得分:3)
尝试一下:
SELECT STUFF
(
(
SELECT CONCAT(',{', ID, ', "', EmpName, '", ', age, '}')
FROM @mytable
FOR XML PATH(''), TYPE
).value('.', 'varchar(max)')
,1
,1
,'['
) + ']';
答案 1 :(得分:1)
您可以使用“选择变量”技巧来构建字符串。
acronyms.keyword
返回值:
declare @JsonesceString varchar(max);
select @JsonesceString = concat(@JsonesceString+','+char(10),'{',ID,',"',EmpName,'",',age,'}')
from @mytable
order by ID;
select concat('[',@JsonesceString,']') as Result;