为什么 JSON_QUERY 发回空值?

ray*_*el5 2 sql-server json

这让我发疯,我不明白我的方法有什么问题。

我在 SQL 中生成一个 JSON 对象,如下所示:

    select @output = (  
         select distinct lngEmpNo, txtFullName
         from tblSecret
         for json path, root('result'), include_null_values
    )
Run Code Online (Sandbox Code Playgroud)

我得到这样的结果:

{"result":[{"lngEmpNo":696969,"txtFullName":"Clinton, Bill"}]}
Run Code Online (Sandbox Code Playgroud)

ISJSON()确认它是有效的 JSON,JSON_QUERY(@OUTPUT, '$.result')并将返回[]JSON 对象的数组部分......很酷!

但是,我正在尝试使用JSON_QUERY来提取特定值:

这给了我一个NULL价值。为什么??????? 我已经尝试过[0],没有[0],当然,txtFullName[0]

SELECT JSON_QUERY(@jsonResponse, '$.result[0].txtFullName');
Run Code Online (Sandbox Code Playgroud)

我以strict,为前缀,SELECT JSON_QUERY(@jsonResponse, 'strict $.result[0].txtFullName');它告诉我:

Msg 13607, Level 16, State 4, Line 29
JSON path is not properly formatted. Unexpected character 't' is found at 
position 18.
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?我的结构有什么问题?

Dav*_*idG 7

JSON_QUERY只会提取一个对象或一个数组。您正在尝试提取单个值,因此需要使用JSON_VALUE. 例如:

SELECT JSON_VALUE(@jsonResponse, '$.result[0].txtFullName');
Run Code Online (Sandbox Code Playgroud)