如何在下面编写SQL查询,以提及使用FOR JSON的表结构和JSON输出。
表结构
CREATE TABLE [dbo].[TreeView](
[AuditId] [int] NOT NULL,
[Id] [int] NOT NULL,
[FatherId] [int] NULL,
[MotherId] [int] NULL,
[EnglishName] [varchar](500) NULL,
[Description] [nchar](10) NULL,
[Image] [varchar](500) NULL,
JSON输出
{ id: 1, title: "Meera Mohaideen", description: "", image: "demo/images/photos/m.png" },
{ id: 2, title: "Rahman Beevi", description: "", image: "demo/images/photos/f.png" },
{ id: 3, title: "Abdul Jabbar", description: "", image: "demo/images/photos/m.png" },
{ id: 4, title: "Jahabar Natchiyal", description: "", image: "demo/images/photos/f.png" },
{ id: 5, parents: [1,2] ,title: "AJhir Ali", description: "", image: "demo/images/photos/m.png" },
{ id: 6, parents: [3, 4], title: "Kamila Begam", description: "", image: "demo/images/photos/f.png" },
{ id: 7, parents: [5, 6], title: "Simra", description: "7", image: "demo/images/photos/f.png" }
查询
SELECT Id AS Id,
CASE WHEN ISNULL(FatherId,0) <> 0 OR ISNULL(MotherId,0) <> 0 THEN
CAST(FatherId AS VARCHAR(25)) + ',' + CAST(MotherId AS VARCHAR(25)) END AS parents,
EnglishName AS title,
[Description] AS Description,
[Image] AS Image FROM TreeView
FOR JSON AUTO
我尝试过,但结果是{“ Id”:5,“父母”:“ 1,2”,“标题”:“ AJhir Ali”} 但我期望这个{“ Id”:5,“父母”:[1,2],“标题”:“ AJhir Ali”}
谢谢