ParseJSON 函数

适用于:画布应用模型驱动应用 Power Platform CLI

解释 JSON 字符串并返回非类型化对象

Description

ParseJSON 函数将分析有效的 JSON 字符串并返回表示 JSON 结构的非类型化对象

如果根据 ECMA-404IETF RFC 8259 中描述的 JavaScript 对象表示法 (JSON) 格式,文本不是有效的 JSON,ParseJSON 函数可能会返回错误。

语法

ParseJSON( JSONString )

  • JSONString – 必需。 以文本表示的 JSON 结构。

转换非类型化对象数据类型

ParseJSON 将返回需要在支持的数据类型中显式转换字段值的非类型化对象。 下表列出了 Power Apps 中的数据类型和对应的 JSON 数据类型以及如何转换。

数据类型 JSON 示例 说明 转换示例
布尔型 { "bool": true } 布尔是 JSON 中的显式类型,可以直接转换。 Boolean( ParseJSON("{ ""bool"": true }").bool )
Color { "color": "#102030" }
{ "r": 255, "g": 128, "b": 0, "a": 0.5 }
JSON 中没有颜色类型。 颜色值可以从 RGBA 整数或十六进制字符串创建。 ColorValue( ParseJSON( "{ ""color"": ""#102030"" }" ).color )
With( { uo: ParseJSON( "{ ""r"": 255, ""g"": 128, ""b"": 0, ""a"": 0.5 }" ) }, RGBA( Value( uo.r ), Value( uo.g ), Value( uo.b ), Value( uo.a ) ) )
货币、数字 { "numbervalue": 123.5 } 数字在 JSON 中直接表示,用句点 ( . ) 作为小数分隔符。 Value( ParseJSON("{ ""numbervalue"": 123.5 }").numbervalue )
日期、日期/时间、时间 { "start": "2022-05-10" }
{ "start": "23:12:49.000" }
JSON 没有日期或时间类型,因此只能将日期和时间表示为字符串。 非类型化对象可以直接从 ISO 8601 格式的字符串转换为日期、时间或日期/时间。 对于其他格式,首先使用 Text() 函数将 JSON 字段转换为文本,然后使用默认将使用当前用户设置的语言的 DateValue()、TimeValue() 或 DateTimeValue() 函数。 DateValue( ParseJSON("{ ""appointment"": ""2022-05-10"" }").appointment )
DateValue( Text( ParseJSON("{ ""appointment"": ""May 5, 2022"" }").appointment ) )
GUID { "id": "123e4567-e89b-12d3-a456-426655440000" } JSON 没有 GUId 的数据类型,因此它们只能表示为字符串。 GUID( ParseJSON("{ ""id"": ""123e4567-e89b-12d3-a456-426655440000"" }").id )
超链接、图像和媒体 { "URI": "https://northwindtraders.com/logo.jpg" } 这些数据类型是文本数据类型,可以转换为文本,然后在 Power Apps 中使用。 Text( ParseJSON("{ ""URI"": ""https://northwindtraders.com/logo.jpg"" }").URI )
单选 { "status": 1 }
{ "status": "Closed" }
选择项显示为本地化字符串,数字为后备。 JSON() 函数将选择项序列化为其后备数字。 没有从数字或字符串到选择项的直接转换,但 Switch()If() 函数可用于文本或数字值。 Switch( Value( ParseJSON( "{ ""status"": 1 }" ).status ), 0, Status.Open, 1, Status.Closed )
记录 { "field": "value" } 没有从 JSON 对象到记录结构的直接转换,但可以从非类型化对象中检索各个字段来形成记录。 { field: Text( ParseJSON( "{ ""field"": ""value"" }" ).field ) }
记录引用 记录引用是数据源特有的,不能序列化或反序列化。 表示唯一键的字段值可以在 JSON 中用于标识可以随后查找的记录。
[ { "id": 1, "name": "one" }, { "id": 2, "name": "two" } ]
[1, 2, 3]
JSON 可以包含数组,数组可以转换成表。 这些值可以是记录数组,也可以是实际上是单列表的值数组。 ParseJSON() 数组只能转换成非类型化对象的单列表,可以原样使用或使用 ForAll() 转换为记录的类型化表。 ForAll( Table( ParseJSON( "[ { ""id"": 1, ""name"": ""one"" }, { ""id"": 2, ""name"": ""two"" } ]" ) ), { id: Value(ThisRecord.Value.id), name: Text(ThisRecord.Value.name) } )
文本 { "stringField": "this is text" } 文本是 JSON 中的显式类型,可以直接转换。 Text( ParseJSON( "{ ""stringField"": ""this is text"" }").stringField )
两个选项 { "available": true }
{ "available": "Yes" }
两个选项显示为本地化字符串,布尔值为后备。 JSON() 函数将两个选项序列化为布尔值。 没有从布尔值、数字或字符串到两个选项的直接转换,但 Switch()If() 函数可用于文本、数字或布尔值。 Switch( Boolean( ParseJSON( "{ ""available"": true }" ).available ), false, Availability.No, true, Availability.Yes )

示例

访问字段值

假定在名为 JsonString 的变量中有以下 JSON 字符串

{ "parent": { "child": "text value" }, "number": 567 }
  1. 以下公式返回文本 text value
    Text( ParseJSON( JsonString ).parent.child )
    
  2. 以下公式返回数字 567
    Value( ParseJSON( JsonString ).number )
    

如果字段名称包含无效的标识符名称,您可以将字段名称放在单引号中。 假定在名为 JsonString 的变量中有以下 JSON 字符串

{ "0": { "child-field": "text value" } }
  1. 以下公式返回文本 text value
    Text( ParseJSON( JsonString ).'0'.'child-field' )
    

空白

假定在名为 JsonString 的变量中有以下 JSON 字符串

{ "text": "text value" , "number": 567, "empty": null }
  1. 尝试访问非现有字段将返回 Blank()。 以下公式返回 true
    IsBlank( Text( ParseJSON( JsonString ).parent.child ) )
    
  2. JSON null 值被视为 Blank()。 以下公式返回 true
    IsBlank( Text( ParseJSON( JsonString ).empty ) )
    

简单数组

假定在名为 JsonString 的变量中有以下 JSON 字符串

{ "array": [1, 2, 3] }
  1. 访问非类型化对象数组字段的单列表中的第二个数字,使用 Value() 转换为数字将返回 2
    Value( Index( ParseJSON( JsonString ).array, 2 ) )
    
  2. 将数组字段中非类型化对象的单列表转换为数字 { Value: 1 }, { Value: 2 }, { Value: 3 } 的单列表:
    ForAll( ParseJSON( JsonString ).array, Value( ThisRecord ) )
    

记录数组

假定在名为 JsonString 的变量中有以下 JSON 字符串

{ "array": [
    { "id": 1, "name": "One"},
    { "id": 2, "name": "Two"}
    ] }
  1. 通过使用 ThisRecord.[fieldname] 访问非类型化对象字段可以直接使用 ForAll() 转换为类型化记录表,然后将其转换为已知类型:

    ForAll( ParseJSON( JsonString ).array, { id: Value(ThisRecord.id), name: Text(ThisRecord.name) })
    

数组到表

  1. 通过使用 Table() 函数将非类型化对象转换为表,将生成一个非类型对象的单列表。 然后需要使用 Value(单列)列访问对象,将其转换为前面所述的类型。

假定在名为 JsonString 的变量中有以下 JSON 字符串

{ "array": [1, 2, 3] }

Table() 将返回一个非类型化对象的单列表,数组中的数字值为单列。

 Set(untypedTable, Table( ParseJSON( JsonString ).array );
 
 Value( Index(untypedTable, 1).Value.Value )
 ```

Given the following JSON string in a variable named `JsonString`
```JSON
{ "array": [
 { "id": 1, "name": "One"},
 { "id": 2, "name": "Two"}
 ] }

Table() 将返回一个非类型化对象的单列表,表示数组中的每个 json 对象。

  Set(untypedTable, Table( ParseJSON( JsonString ).array );
  
  Text( Index(untypedTable, 1).Value.name )