- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 JSON API 调用从 Web 服务读取数据。它以(恕我直言)一种不寻常的格式返回数据:
{
"statusCode": 200,
"headers": {
"Transfer-Encoding": "chunked",
"Content-Type": "application/json; charset=UTF-8",
},
"body": {
"columns": {
"column": [
{
"id": -1,
"val": null,
"name": "COLUMNA",
"caption": "Column A",
"value": null
},
{
"id": 106009714,
"val": null,
"name": "COLUMNB",
"caption": "Column B",
"value": null
},
{
"id": 106010949,
"val": null,
"name": "COLUMNC",
"caption": "Column C",
"value": null
},
{
"id": 106009719,
"val": null,
"name": "COLUMND",
"caption": "Column D",
"value": null
},
"records": {
"record": [
{
"field": [
{
"id": -1,
"val": "390011768",
"name": "COLUMNA",
"caption": null,
"value": null
},
{
"id": 106009714,
"val": "Lorem ipsum",
"name": "COLUMNB",
"caption": null,
"value": null
},
{
"id": 106010949,
"val": "Nulla mauris ante",
"name": "COLUMNC",
"caption": null,
"value": null
},
{
"id": 106009719,
"val": "15-01-2019 12:46",
"name": "COLUMND",
"caption": null,
"value": null
}
]
},
{
"field": [
{
"id": -1,
"val": "390012438",
"name": "COLUMNA",
"caption": null,
"value": null
},
{
"id": 106009714,
"val": "Vestibulum ligula",
"name": "COLUMNB",
"caption": null,
"value": null
},
{
"id": 106010949,
"val": "Nulla elit orci",
"name": "COLUMNC",
"caption": null,
"value": null
},
{
"id": 106009719,
"val": "27-03-2019 14:17",
"name": "COLUMND",
"caption": null,
"value": null
}
]
},
{
"field": [
{
"id": -1,
"val": "390013343",
"name": "COLUMNA",
"caption": null,
"value": null
},
{
"id": 106009714,
"val": "Nunc magna risus",
"name": "COLUMNB",
"caption": null,
"value": null
},
{
"id": 106010949,
"val": "Vivamus rutrum",
"name": "COLUMNC",
"caption": null,
"value": null
},
{
"id": 106009719,
"val": "23-07-2019 13:17",
"name": "COLUMND",
"caption": null,
"value": null
}
]
}
]
}
}
}
第一个 block 描述列标题,后面是另一个 block ,其中每行定义为字段数组。我的目标是将其转换为以下(恕我直言)更常用的格式:
{
{
"Column A":"390011768",
"Column B":"Lorem ipsum",
"Column C":"Nulla mauris ante",
"Column D":"27-03-2019 14:17"
},
etc
}
我已经想出了一个解决方案,但感觉很困惑/错误,并且涉及字符串对象和数组之间的大量转换。其代码如下:
{
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"CreateJSON": {
"inputs": {
"body": {
"columns": {
"column": [
{
"caption": "Column A",
"id": -1,
"name": "COLUMNA",
"val": null,
"value": null
},
{
"caption": "Column B",
"id": 106009714,
"name": "COLUMNB",
"val": null,
"value": null
},
{
"caption": "Column C",
"id": 106010949,
"name": "COLUMNC",
"val": null,
"value": null
},
{
"caption": "Column D",
"id": 106009719,
"name": "COLUMND",
"val": null,
"value": null
}
]
},
"records": {
"record": [
{
"field": [
{
"caption": null,
"id": -1,
"name": "COLUMNA",
"val": "390011768",
"value": null
},
{
"caption": null,
"id": 106009714,
"name": "COLUMNB",
"val": "Lorem ipsum",
"value": null
},
{
"caption": null,
"id": 106010949,
"name": "COLUMNC",
"val": "Nulla mauris ante",
"value": null
},
{
"caption": null,
"id": 106009719,
"name": "COLUMND",
"val": "15-01-2019 12:46",
"value": null
}
]
},
{
"field": [
{
"caption": null,
"id": -1,
"name": "COLUMNA",
"val": "390012438",
"value": null
},
{
"caption": null,
"id": 106009714,
"name": "COLUMNB",
"val": "Vestibulum ligula",
"value": null
},
{
"caption": null,
"id": 106010949,
"name": "COLUMNC",
"val": "Nulla elit orci",
"value": null
},
{
"caption": null,
"id": 106009719,
"name": "COLUMND",
"val": "27-03-2019 14:17",
"value": null
}
]
},
{
"field": [
{
"caption": null,
"id": -1,
"name": "COLUMNA",
"val": "390013343",
"value": null
},
{
"caption": null,
"id": 106009714,
"name": "COLUMNB",
"val": "Nunc magna risus",
"value": null
},
{
"caption": null,
"id": 106010949,
"name": "COLUMNC",
"val": "Vivamus rutrum",
"value": null
},
{
"caption": null,
"id": 106009719,
"name": "COLUMND",
"val": "23-07-2019 13:17",
"value": null
}
]
}
]
}
},
"headers": {
"Content-Type": "application/json; charset=UTF-8",
"Date": "Tue, 22 Sep 2020 17:26:18 GMT",
"Transfer-Encoding": "chunked"
},
"statusCode": 200
},
"runAfter": {},
"type": "Compose"
},
"Create_HTML_table": {
"inputs": {
"format": "HTML",
"from": "@variables('RecordArray')"
},
"runAfter": {
"ReconstituteArray": [
"Succeeded"
]
},
"type": "Table"
},
"FlattenArray": {
"inputs": {
"variables": [
{
"name": "ArrayString",
"type": "string",
"value": "@{replace(join(variables('RowAccumulator'), ','),'],[',',')}"
}
]
},
"runAfter": {
"RowLoop": [
"Succeeded"
]
},
"type": "InitializeVariable"
},
"HeaderCollection": {
"actions": {
"ColumnHeadings": {
"inputs": {
"caption": "@items('HeaderCollection')['caption']",
"name": "@items('HeaderCollection')['name']"
},
"runAfter": {},
"type": "Compose"
}
},
"foreach": "@outputs('CreateJSON')['body']['columns']['column']",
"runAfter": {
"CreateJSON": [
"Succeeded"
]
},
"type": "Foreach"
},
"InitFieldAccumulator": {
"inputs": {
"variables": [
{
"name": "FieldAccumulator",
"type": "array"
}
]
},
"runAfter": {
"InitRowString": [
"Succeeded"
]
},
"type": "InitializeVariable"
},
"InitRowAccumulator": {
"inputs": {
"variables": [
{
"name": "RowAccumulator",
"type": "array"
}
]
},
"runAfter": {
"InitFieldAccumulator": [
"Succeeded"
]
},
"type": "InitializeVariable"
},
"InitRowString": {
"inputs": {
"variables": [
{
"name": "RowString",
"type": "string"
}
]
},
"runAfter": {
"HeaderCollection": [
"Succeeded"
]
},
"type": "InitializeVariable"
},
"ReconstituteArray": {
"inputs": {
"variables": [
{
"name": "RecordArray",
"type": "array",
"value": "@json(variables('ArrayString'))"
}
]
},
"runAfter": {
"FlattenArray": [
"Succeeded"
]
},
"type": "InitializeVariable"
},
"RowLoop": {
"actions": {
"FieldLoop": {
"actions": {
"Compose": {
"inputs": "@setProperty(item(), 'caption', body('MatchCaption')[0]['caption'])",
"runAfter": {
"MatchCaption": [
"Succeeded"
]
},
"type": "Compose"
},
"FieldAccumulator": {
"inputs": {
"name": "FieldAccumulator",
"value": "@outputs('Compose')"
},
"runAfter": {
"Compose": [
"Succeeded"
]
},
"type": "AppendToArrayVariable"
},
"MatchCaption": {
"inputs": {
"from": "@outputs('ColumnHeadings')",
"where": "@equals(item()['name'], items('FieldLoop')['name'])"
},
"runAfter": {},
"type": "Query"
}
},
"foreach": "@items('RowLoop')['field']",
"runAfter": {},
"runtimeConfiguration": {
"concurrency": {
"repetitions": 1
}
},
"type": "Foreach"
},
"FlattenRow": {
"inputs": {
"name": "RowString",
"value": "@{replace(string(body('Select')), '},{', ',')}"
},
"runAfter": {
"Select": [
"Succeeded"
]
},
"type": "SetVariable"
},
"Reset_FieldAccumulator": {
"inputs": {
"name": "FieldAccumulator",
"value": []
},
"runAfter": {
"RowAccumulator": [
"Succeeded"
]
},
"type": "SetVariable"
},
"RowAccumulator": {
"inputs": {
"name": "RowAccumulator",
"value": "@variables('RowString')"
},
"runAfter": {
"FlattenRow": [
"Succeeded"
]
},
"type": "AppendToArrayVariable"
},
"Select": {
"inputs": {
"from": "@variables('FieldAccumulator')",
"select": {
"@{item()['caption']}": "@item()['val']"
}
},
"runAfter": {
"FieldLoop": [
"Succeeded"
]
},
"type": "Select"
}
},
"foreach": "@outputs('CreateJSON')['body']['records']['record']",
"runAfter": {
"InitRowAccumulator": [
"Succeeded"
]
},
"runtimeConfiguration": {
"concurrency": {
"repetitions": 1
}
},
"type": "Foreach"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {},
"triggers": {
"Recurrence": {
"recurrence": {
"frequency": "Day",
"interval": 1
},
"type": "Recurrence"
}
}
},
"parameters": {}
}
首先,我创建一个 JSON 有效负载(模拟从 API 返回的内容)。然后我有一个循环,它创建一个包含列名称和标题的数组,稍后我将使用它来提供友好的列名称。
第二个循环迭代“记录”元素,这些元素本身就是数组。它使用 FilterArray 查找正确的标题,使用 Compose 创建 JSON 片段,然后将其存储到每个字段。
处理完一行的所有字段后,我使用 Select 来隔离我需要的值,然后将其转换为“正常”JSON 元素并存储它。
处理完所有行后,我会执行更多处理,通过反复试验来得出可与“创建 HTML 表”功能配合使用的内容。
虽然这种方法有效,但似乎太复杂了。但是我找不到另一种有效的方法。我是逻辑应用程序的新手,但不是编程。任何人都可以提供更好/更惯用的方法来实现这一目标吗?或者这种类型的处理最好外包给某个函数吗?
PS 逻辑应用程序将在小型数据集(两位数)上运行,因此我不太担心性能,但想必更惯用的解决方案会表现得更好。
最佳答案
您可以使用Liquid templates定义复杂的 json 转换,如 docs 的建议:
For advanced and complex JSON to JSON transformations that have elements such as iterations, control flows, and variables, create and use templates that describe these transformations by using the Liquid open-source template language.
创建液体模板的一种简单方法是使用 Visual Studio Code与 some plugins .
主要好处是,以这种方式创作转换比使用逻辑应用设计器要容易得多。此外,转换逻辑与业务逻辑是分离的。
您还可以使用 Liquid 模板将 JSON 转换为文本,因此直接转换为 html 是一种选择。
顺便说一句,您输入的 json 似乎无效,因此我无法提供简单的示例。
关于arrays - 在逻辑应用程序中处理复杂 JSON 数据的正确/惯用方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64028172/
当我尝试加载库 Raster 时,我收到如下错误: 错误:inDL(x, as.logic(local), as.logic(now), ...) 中的“raster”的包或命名空间加载失败:无法加载
当我尝试加载库 Raster 时,我收到如下错误: 错误:inDL(x, as.logic(local), as.logic(now), ...) 中的“raster”的包或命名空间加载失败:无法加载
望着help section about_Comparison_Operators of PowerShell我是这样理解的: PS C:\> $false,$false -eq $true PS C
我刚刚修改了旧代码,现在似乎没有任何效果。请您指导我哪里出错了。 一些不起作用的事情是: 以前,焦点始终停留在屏幕上唯一的输入字段上。 (现在不行了),代码中的 if else 条件也不起作用。 On
请帮我找到一个使用普通 'ol javascript 的解决方案(我无法使用外部框架)。此外,CSS :hover 选择器不适用于现实世界的实现。 注册事件发生的事情设置所有调用最后注册事件数组项。
我想创建一个软件来为残障 child 交通规划公交路线(及其最佳载客量)。 这些总线具有以下规范: m 个座位(最多 7 个 - 因为有司机和助理) o 轮椅“座位”(最多 4 个) 固定的最大负载量
有人能帮我吗?似乎我的 for 逻辑根本不起作用,因为它一直在上午 12:00 返回我的开始时间 这是我的代码 Sub forlogic() Dim i As Single Dim t
我正在尝试设置 OR两个切片器过滤器之间的逻辑。两个切片器来自相同的数据集。以下是更多详细信息: 我的源表: 带切片器的视觉效果: 我的目标是,如果我从切片器 1 和切片器 2 中选择任何值,我的视觉
我有以下 C 语句: int res = x & (x ^ y); 有没有办法做同样的事情,但每次只使用一次x和y? 例如: x | (~x & y) == x | y 最佳答案 是的,通过扩展 xo
我正在创建 Azure 逻辑应用程序以将新的 Sharepoint 文件添加到 Azure Blob。 Sharepoint 由我的公司运行,我使用我的凭据登录来为逻辑应用程序创建 Sharepoin
我有一个问题要求为给定函数合成最简单的乘积表达式总和。基本上,如果 AB == CD,则函数为 1,否则为 0,结果如下: (!A && !B && !C && !D) || (!A && B &&
我正在尝试确定是否可以在不溢出的情况下计算两个 32 位整数的总和,同时仅使用某些按位运算符和其他运算符。因此,如果整数 x 和 y 可以相加而不会溢出,则以下代码应返回 1,否则返回 0。 ((((
处理乍一看需要许多嵌套 if 语句的复杂业务逻辑的好方法是什么? 例子: 折扣券。可能: 1a) 超值折扣 1b) 百分比折扣 2a) 正常折扣 2b) 累进折扣 3a) 需要访问优惠券 3b) 不需
假设我有一个“numbers”对象数组,其中包含“startNo”整数和“endNo”整数。 数组中可以有多个“数字”,我想获取一个包含修改对象的新数组,该数组仅具有不重叠的范围。 例如:如果数组有:
我在这个问题上遇到了困难。我正在使用 JavaScript。 我有一个文本区域,用于检测 @ 输入并将其位置存储在数组中。 var input = "@a @b @c" //textarea var
默认 IN 使用 OR 基本逻辑。有没有办法在范围内使用 AND 基本逻辑。 例如下面的查询 SELECT ItemId,CategoryID FROM ItemCategories WHERE Ca
我想在您将鼠标悬停在网站图像上时添加叠加层。我在这里实现了这个,它工作正常http://jsfiddle.net/stujLbjh/ 这是js代码: var divs = document.query
这个问题在这里已经有了答案: Which is faster: x>2 是否比 x>>31 快?换句话说,sar x, 2 是否比 sar x, 31 快?我做了一些简单的测试,他们似乎有相同的速度
我有grails criteriaQuery,我在这里再次检查OR逻辑,就像这样一个状态变量: or { eq("status", Status.ONE) eq("status",
我有grails criteriaQuery,我在这里再次检查OR逻辑,就像这样一个状态变量: or { eq("status", Status.ONE) eq("status",
我是一名优秀的程序员,十分优秀!