StringToArray (Azure Cosmos DB)StringToArray (Azure Cosmos DB)

适用于: SQL API

返回已转换为数组的表达式。Returns expression translated to an Array. 如果表达式无法转换,则返回未定义的表达式。If expression cannot be translated, returns undefined.

语法Syntax

StringToArray(<str_expr>)  

参数Arguments

str_exprstr_expr
是要解析为 JSON Array 表达式的字符串表达式。Is a string expression to be parsed as a JSON Array expression.

返回类型Return types

返回一个数组表达式或未定义的表达式。Returns an array expression or undefined.

备注Remarks

嵌套字符串值必须用双引号写成有效的 JSON。Nested string values must be written with double quotes to be valid JSON. 有关 JSON 格式的详细信息,请参阅 json.org。此系统函数不会使用索引。For details on the JSON format, see json.org. This system function will not utilize the index.

示例Examples

以下示例演示 StringToArray 在不同类型中的行为方式。The following example shows how StringToArray behaves across different types.

下面是输入有效的示例。The following are examples with valid input.

SELECT 
    StringToArray('[]') AS a1, 
    StringToArray("[1,2,3]") AS a2,
    StringToArray("[\"str\",2,3]") AS a3,
    StringToArray('[["5","6","7"],["8"],["9"]]') AS a4,
    StringToArray('[1,2,3, "[4,5,6]",[7,8]]') AS a5

下面是结果集。Here is the result set.

[{"a1": [], "a2": [1,2,3], "a3": ["str",2,3], "a4": [["5","6","7"],["8"],["9"]], "a5": [1,2,3,"[4,5,6]",[7,8]]}]

下面是输入无效的示例。The following is an example of invalid input.

在数组中使用单引号不是有效的 JSON。Single quotes within the array are not valid JSON. 即使它们在查询中有效,系统也不会将其解析为有效数组。Even though they are valid within a query, they will not parse to valid arrays. 必须将数组字符串中的字符串转义为 "[\"\"]",否则其引号必须为单个 '[""]'。Strings within the array string must either be escaped "[\"\"]" or the surrounding quote must be single '[""]'.

SELECT
    StringToArray("['5','6','7']")

下面是结果集。Here is the result set.

[{}]

下面是输入无效的示例。The following are examples of invalid input.

传递的表达式将会解析为 JSON 数组;下面的示例不会计算为类型数组,因此返回未定义。The expression passed will be parsed as a JSON array; the following do not evaluate to type array and thus return undefined.

SELECT
    StringToArray("["),
    StringToArray("1"),
    StringToArray(NaN),
    StringToArray(false),
    StringToArray(undefined)

下面是结果集。Here is the result set.

[{}]

后续步骤Next steps