- 概述
- 加密
- 数据库
- Java
- Python
- 网页 API
用于将 JSON 字符串或示例反序列化为可配置输出对象类型的活动。
UiPath.WebAPI.Activities.DeserializeJson
描述
将 JSON 字符串或示例反序列化为 JSON 对象。默认情况下,结果是JObject ,但您可以使用“输出类型”属性选择不同的输出类型。
项目兼容性
Windows - 旧版 | Windows | 跨平台
先决条件
要使用数据映射配置“序列化器设置”属性,请使用 Studio 25.10 或更高版本。在 Studio 的早期版本中,该属性会改用表达式编辑器窗口。
Windows、跨平台配置
- JSON String - The string containing the JSON to be deserialized. The JSON should be written without indents, and between double quotes. Strings inside the JSON should be written with single quotes. For example, the following JSON is valid: "{'TenancyName': 'Documentation', 'UsernameOrEmail': 'Documentation', 'Password': 'Documentation'}".
- JSON 示例- 用于生成输出对象属性的 JSON 示例。示例必须为 JSON 格式。该活动会将此示例转换为您可以在设计时使用的类似对象。
高级选项
输入
- 序列化器设置- 反序列化 JSON 字符串时要使用的自定义反序列化器设置。当您要完全控制 JSON 字符串的反序列化方式时,请使用此属性。
- 输出类型- 此为下拉列表,用于让您选择要反序列化的对象的参数类型。
备注:
在 Studio Web 中,从“JSON 示例”属性生成输出结构会使“输出类型”属性变为只读。
输出
- JSON 对象- 反序列化的 JSON,作为在“输出类型”属性中选择的类型的对象返回。使用默认类型时,结果是
Newtonsoft.Json.Linq.JObject变量。阅读有关Newtonsoft.Json.Linq.JObject对象的更多信息。
JSON 示例使用示例
“JSON 示例”属性在设计时根据代表性 JSON 代码片段生成输出对象结构。提供示例后,该活动会推断出匹配类型,并将其分配给“输出类型”属性,因此您可以在下游活动中按名称引用反序列化的值,而无需使用SelectToken进行查询。
“JSON 示例”属性是一个设计时帮助程序。此功能在 Studio Web 和支持自定义类型推理的 Studio 版本中可用。当这些功能不可用时,该属性将隐藏。
为了举例说明如何使用JSON 示例,我们从 JSON 字符串中提取人员的姓名和城市:
-
将“反序列化 JSON”活动添加到工作流中。
-
在“JSON 字符串”字段中,输入要反序列化的 JSON。例如:
"{'name': 'John Doe', 'age': 30, 'address': {'city': 'New York'}}"。 -
在“JSON 示例”字段中,粘贴具有相同结构的代表性示例:
{ "name": "John Doe", "age": 30, "address": { "city": "New York" } }{ "name": "John Doe", "age": 30, "address": { "city": "New York" } }该活动读取示例,并生成反映此结构的输出对象。输出类型属性将更新为生成的类型。
-
在后续活动中,直接通过示例中定义的属性名称(例如
JsonObject.name和JsonObject.address.city引用反序列化的值,而不是使用JsonObject.SelectToken("name")。
结果
输出对象公开了示例中的属性,因此JsonObject.name返回John Doe且JsonObject.address.city返回New York而无需使用SelectToken查询 JSON。
使用序列化器设置的示例
当“序列化器设置”属性没有可用的数据映射时,请使用以下代码片段之一在“表达式编辑器”中对其进行配置。有关更多信息,请参阅Newtonsoft 文档。
VB.NET
New JsonDeserializationSettings With {
.MissingMemberHandling = JsonMissingMemberHandling.Ignore,
.NullValueHandling = JsonNullValueHandling.Ignore,
.DateParseHandling = JsonDateParseHandling.DateTime,
.DateTimeZoneHandling = JsonDateTimeZoneHandling.Utc,
.FloatParseHandling = JsonFloatParseHandling.Double
}
New JsonDeserializationSettings With {
.MissingMemberHandling = JsonMissingMemberHandling.Ignore,
.NullValueHandling = JsonNullValueHandling.Ignore,
.DateParseHandling = JsonDateParseHandling.DateTime,
.DateTimeZoneHandling = JsonDateTimeZoneHandling.Utc,
.FloatParseHandling = JsonFloatParseHandling.Double
}
C#
new JsonDeserializationSettings
{
MissingMemberHandling = JsonMissingMemberHandling.Ignore,
NullValueHandling = JsonNullValueHandling.Ignore,
DateParseHandling = JsonDateParseHandling.DateTime,
DateTimeZoneHandling = JsonDateTimeZoneHandling.Utc,
FloatParseHandling = JsonFloatParseHandling.Double
}
new JsonDeserializationSettings
{
MissingMemberHandling = JsonMissingMemberHandling.Ignore,
NullValueHandling = JsonNullValueHandling.Ignore,
DateParseHandling = JsonDateParseHandling.DateTime,
DateTimeZoneHandling = JsonDateTimeZoneHandling.Utc,
FloatParseHandling = JsonFloatParseHandling.Double
}
Windows - 旧版配置
属性面板
常见
- “显示名称”- 活动的显示名称。
输入
- JSON 示例- 输出结构的 JSON 示例。该活动会将此示例转换为您可以在设计时使用的类似对象。
- Json 字符串- 此为包含要反序列化的 JSON 数组的字符串。在双引号之间编写不带缩进的 JSON。JSON 中的字符串应使用单引号编写。下例展示了有效的 JSON:“{'TenancyName': 'Documentation', 'UsernameOrEmail': 'Documentation', 'Password': 'Documentation'}”。
其他
- “私有”- 选中后将不再以“Verbose”级别记录变量和参数的值。
- “类型参数” - 此为下拉列表,用于选择返回的输出类型。系统会将所提供的 JSON 字符串转换为所选类型的对象。
输出
- Json 对象- 反序列化的 JSON,作为
Newtonsoft.Json.Linq.JObject变量。阅读有关Newtonsoft.Json.Linq.JObject对象的更多信息。
使用“反序列化 Json”活动的示例
“反序列化 Json”活动用于从 JSON 文件和字符串中提取数据。
为了举例说明如何使用此活动,我们创建了一个示例,该示例使用“反序列化 JSON”活动从 JSON 文件中提取人员的年龄。
此自动化流程的构建方法如下:
- 打开 Studio ,创建一个新“流程”。
- Drag a Sequence to the Workflow Designer.
-
创建以下变量:
变量名称 变量类型 默认值 JsonText字符串 JsonObjJObject Age泛型值
-
- Drag a Read Text File activity inside the sequence container.
- In the Properties panel, add the value
"JsonString.txt"in the FileName field. - Add the variable
JsonTextin the Content field.
- In the Properties panel, add the value
- 将“反序列化 JSON”活动添加到“读取文本文件”活动之后。
- In the Properties panel, add the variable
JsonTextin the JsonString field and the variableJsonObjin the JsonObject field. - Select the value
Newtonsoft.Json.Linq.JObjectfrom the TypeArgument drop-down list.
- In the Properties panel, add the variable
- Place an Assign activity under the Deserialize JSON activity.
- Add the variable
Agein the To field and the expressionJsonObj.SelectToken("age").ToStringin the Value field.
- Add the variable
- 将“写入行”活动拖动到“分配”活动之后。
- 在“文本”字段中添加“
Age”变量。
- 在“文本”字段中添加“
- Run the process. The robot extracts the age value from the
.jsonfile and displays it in the Output panel.
您可在此处下载示例。