我想将以下内容序列化为对象的字符串 not 的简单数组。
DECLARE @json NVARCHAR(MAX) = '["Developer", "Designer"]'
SELECT x.position
FROM (
SELECT position
FROM OPENJSON(@json)
WITH (position nvarchar(50) '$')
) x
FOR JSON PATH
运行上述命令时,我得到:[{position: "Developer"}, {position: "Designer}]
但是我想要:["Developer", "Designer"]
我该怎么做?
答案 0 :(得分:2)
我认为您无法使用“ FOR JSON”来实现。
回到字符串操作:
DECLARE @json NVARCHAR(MAX) = '["Developer", "Designer"]'
SELECT '["'+STRING_AGG( STRING_ESCAPE(x.position, 'json'),'", "') + '"]'
FROM (
SELECT position
FROM OPENJSON(@json)
WITH (position nvarchar(50) '$')
) x
答案 1 :(得分:-1)
尝试像这样声明您的json ...
DECLARE @json NVARCHAR(MAX) = '{{"position": "Developer"}, {"position": "Designer"}}'
hth
-n