我试图获得的是一个简单的SQL语句来构建:
{"status":{"code":404,"message":"Not found"},"otherthing":20}
Run Code Online (Sandbox Code Playgroud)
如果我设置为:
DECLARE @ReturnJSON nvarchar(max)
SET @ReturnJSON = (
SELECT (
SELECT 404 as [code]
,'Not found' as [message]
FOR JSON PATH ) as [status]
, 20 as [otherthing]
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER) ;
SELECT @ReturnJSON
Run Code Online (Sandbox Code Playgroud)
我在数组包装器下获得第二级,如下所示:
{"status":[{"code":404,"message":"Not found"}],"otherthing":20}
Run Code Online (Sandbox Code Playgroud)
但如果我WITHOUT_ARRAY_WRAPPER在第二级添加......
DECLARE @ReturnJSON nvarchar(max)
SET @ReturnJSON = (
SELECT (
SELECT 404 as [code]
,'Not found' as [message]
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER ) as [status]
, 20 as [otherthing]
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER) ;
SELECT @ReturnJSON
Run Code Online (Sandbox Code Playgroud)
有趣的事情发生: …