本文共 2937 字,大约阅读时间需要 9 分钟。
从 JSON 中删除方括号 - WITHOUT_ARRAY_WRAPPER 选项Remove Square Brackets from JSON - WITHOUT_ARRAY_WRAPPER Option
06/03/2020
本文内容
适用于:Applies to: SQL Server 2016 (13.x)SQL Server 2016 (13.x)SQL Server 2016 (13.x)SQL Server 2016 (13.x) 及更高版本适用于:Applies to: SQL Server 2016 (13.x)SQL Server 2016 (13.x)SQL Server 2016 (13.x)SQL Server 2016 (13.x) and later
若要删除默认括住 FOR JSON 子句的 JSON 输出的方括号,请指定 WITHOUT_ARRAY_WRAPPER 选项。To remove the square brackets that surround the JSON output of the FOR JSON clause by default, specify the WITHOUT_ARRAY_WRAPPER option. 将此选项用于单行结果,生成单个 JSON 对象作为输出,而不是生成具有单个元素的数组。Use this option with a single-row result to generate a single JSON object as output instead of an array with a single element.
如果将此选项用于多行结果,生成的输出将不会是有效的 JSON,因为存在多个元素并且缺少方括号。If you use this option with a multiple-row result, the resulting output is not valid JSON because of the multiple elements and the missing square brackets.
示例(单行结果)Example (single-row result)
以下示例介绍了在指定和未指定 WITHOUT_ARRAY_WRAPPER 选项的情况下 FOR JSON 子句的输出。The following example shows the output of the FOR JSON clause with and without the WITHOUT_ARRAY_WRAPPER option.
查询Query
SELECT 2015 as year, 12 as month, 15 as day
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
指定 结果 with the 结果Result with the WITHOUT_ARRAY_WRAPPER option
{
"year": 2015,
"month": 12,
"day": 15
}
未指定 WITHOUT_ARRAY_WRAPPER 选项的结果(默认)Result (default) without the WITHOUT_ARRAY_WRAPPER option
[{
"year": 2015,
"month": 12,
"day": 15
}]
示例(多行结果)Example (multiple-row result)
下面又通过一个示例介绍了在指定 FOR JSON 选项的情况下 WITHOUT_ARRAY_WRAPPER 选项。Here's another example of a FOR JSON clause with and without the WITHOUT_ARRAY_WRAPPER option. 本示例生成多行结果。This example produces a multiple-row result. 输出不是有效的 JSON,因为存在多个元素并且缺少方括号。The output is not valid JSON because of the multiple elements and the missing square brackets.
查询Query
SELECT TOP 3 SalesOrderNumber, OrderDate, Status
FROM Sales.SalesOrderHeader
ORDER BY ModifiedDate
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
指定 结果 with the 结果Result with the WITHOUT_ARRAY_WRAPPER option
{
"SalesOrderNumber": "SO43662",
"OrderDate": "2011-05-31T00:00:00",
"Status": 5
}, {
"SalesOrderNumber": "SO43661",
"OrderDate": "2011-05-31T00:00:00",
"Status": 5
}, {
"SalesOrderNumber": "SO43660",
"OrderDate": "2011-05-31T00:00:00",
"Status": 5
}
未指定 WITHOUT_ARRAY_WRAPPER 选项的结果(默认)Result (default) without the WITHOUT_ARRAY_WRAPPER option
[{
"SalesOrderNumber": "SO43662",
"OrderDate": "2011-05-31T00:00:00",
"Status": 5
}, {
"SalesOrderNumber": "SO43661",
"OrderDate": "2011-05-31T00:00:00",
"Status": 5
}, {
"SalesOrderNumber": "SO43660",
"OrderDate": "2011-05-31T00:00:00",
"Status": 5
}]
详细了解 SQL Server 和 Azure SQL 数据库中的 JSONLearn more about JSON in SQL Server and Azure SQL Database
Microsoft 视频Microsoft videos
有关 SQL Server 和 Azure SQL 数据库中内置 JSON 支持的视频介绍,请观看以下视频:For a visual introduction to the built-in JSON support in SQL Server and Azure SQL Database, see the following videos:
另请参阅See Also
转载地址:https://blog.csdn.net/weixin_32473663/article/details/115541016 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!