如果执行此操作,则可以获取数组中的第一个值。但是如何获取列表中的所有值?
SELECT
JSON_VALUE('{"Distributor": [5030, 4000, 1231]}', '$.Distributor[0]') AS result;
答案 0 :(得分:1)
SELECT value
FROM OPENJSON('{"Distributor": [5030, 4000, 1231]}','$.Distributor')
'$.Distributor'
是开始寻找数组的“开始路径”
答案 1 :(得分:0)
您可以看一下这篇文章: https://docs.microsoft.com/fr-fr/sql/relational-databases/json/json-data-sql-server?view=sql-server-2017
使用功能Openjson
和Outer apply
提供的示例:
DECLARE @json NVARCHAR(MAX)
SET @json =
N'[
{ "id" : 2,"info": { "name": "John", "surname": "Smith" }, "age": 25 },
{ "id" : 5,"info": { "name": "Jane", "surname": "Smith", "skills": ["SQL", "C#", "Azure"] }, "dob": "2005-11-04T12:00:00" }
]'
SELECT *
FROM OPENJSON(@json)
WITH (id int 'strict $.id',
firstName nvarchar(50) '$.info.name', lastName nvarchar(50) '$.info.surname',
age int, dateOfBirth datetime2 '$.dob',
skills nvarchar(max) '$.info.skills' as json)
outer apply openjson( skills )
with ( skill nvarchar(8) '$' )
带有提供的json的编辑代码:
DECLARE @json NVARCHAR(MAX)
SET @json = N'{"Distributor": [5030, 4000, 1231]}'
SELECT Distributor
FROM OPENJSON(@json)
WITH (Distributors nvarchar(max) '$.Distributor' as json)
outer apply openjson( Distributors )
with ( Distributor int '$' )
结果:
Distributor
5030
4000
1231