- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个如下所示的 JSON 文件:
{
"id": 10011,
"title": "Test procedure",
"slug": "slug",
"url": "http://test.test",
"email": "test@test.com",
"link": "http://test.er",
"subject": "testing",
"level": 1,
"disciplines": [
"discipline_a",
"discipline_b",
"discipline_c"
],
"areas": [
"area_a",
"area_b"
]
},
我尝试使用以下命令将其转换为 CSV 文件:
(Get-Content "PATH_TO\test.json" -Raw | ConvertFrom-Json)| Convertto-CSV -NoTypeInformation | Set-Content "PATH_TO\test.csv"
但是,对于学科和领域,我在生成的 CSV 文件中获取 System.Object[]。
有没有办法将所有这些嵌套值作为单独的列放入 CSV 文件中,例如area_1、area_2 等。学科也是如此。
最佳答案
2017-11-20,完全重写函数以提高性能并添加功能为-ArrayBase
以及对 PSStandardMembers 和分组对象的支持。
递归地展平包含数组、哈希表和(自定义)对象的对象。所提供的 objects will be aligned 的所有添加属性与其余对象。
需要 PowerShell 版本 2 或更高版本。
Function Flatten-Object { # Version 00.02.12, by iRon
[CmdletBinding()]Param (
[Parameter(ValueFromPipeLine = $True)][Object[]]$Objects,
[String]$Separator = ".", [ValidateSet("", 0, 1)]$Base = 1, [Int]$Depth = 5, [Int]$Uncut = 1,
[String[]]$ToString = ([String], [DateTime], [TimeSpan]), [String[]]$Path = @()
)
$PipeLine = $Input | ForEach {$_}; If ($PipeLine) {$Objects = $PipeLine}
If (@(Get-PSCallStack)[1].Command -eq $MyInvocation.MyCommand.Name -or @(Get-PSCallStack)[1].Command -eq "<position>") {
$Object = @($Objects)[0]; $Iterate = New-Object System.Collections.Specialized.OrderedDictionary
If ($ToString | Where {$Object -is $_}) {$Object = $Object.ToString()}
ElseIf ($Depth) {$Depth--
If ($Object.GetEnumerator.OverloadDefinitions -match "[\W]IDictionaryEnumerator[\W]") {
$Iterate = $Object
} ElseIf ($Object.GetEnumerator.OverloadDefinitions -match "[\W]IEnumerator[\W]") {
$Object.GetEnumerator() | ForEach -Begin {$i = $Base} {$Iterate.($i) = $_; $i += 1}
} Else {
$Names = If ($Uncut) {$Uncut--} Else {$Object.PSStandardMembers.DefaultDisplayPropertySet.ReferencedPropertyNames}
If (!$Names) {$Names = $Object.PSObject.Properties | Where {$_.IsGettable} | Select -Expand Name}
If ($Names) {$Names | ForEach {$Iterate.$_ = $Object.$_}}
}
}
If (@($Iterate.Keys).Count) {
$Iterate.Keys | ForEach {
Flatten-Object @(,$Iterate.$_) $Separator $Base $Depth $Uncut $ToString ($Path + $_)
}
} Else {$Property.(($Path | Where {$_}) -Join $Separator) = $Object}
} ElseIf ($Objects -ne $Null) {
@($Objects) | ForEach -Begin {$Output = @(); $Names = @()} {
New-Variable -Force -Option AllScope -Name Property -Value (New-Object System.Collections.Specialized.OrderedDictionary)
Flatten-Object @(,$_) $Separator $Base $Depth $Uncut $ToString $Path
$Output += New-Object PSObject -Property $Property
$Names += $Output[-1].PSObject.Properties | Select -Expand Name
}
$Output | Select ([String[]]($Names | Select -Unique))
}
}; Set-Alias Flatten Flatten-Object
<Object[]> Flatten-Object [-Separator <String>] [-Base "" | 0 | 1] [-Depth <Int>] [-Uncut<Int>] [ToString <Type[]>]
或者:
Flatten-Object <Object[]> [[-Separator] <String>] [[-Base] "" | 0 | 1] [[-Depth] <Int>] [[-Uncut] <Int>] [[ToString] <Type[]>]
<强> -Object[] <Object[]>
要压平的对象(或多个对象)。
<强> -Separator <String>
(默认: .
)
递归属性名称之间使用的分隔符。 .
<强> -Depth <Int>
(默认: 5
)
展平递归属性的最大深度。任何负值都将导致无限深度,并可能导致无限循环。
<强> -Uncut <Int>
(默认: 1
)
未切割更多对象属性的对象迭代次数将仅限于 DefaultDisplayPropertySet
。任何负值都会显示所有对象的所有属性。
<强> -Base "" | 0 | 1
(默认: 1
)
嵌入数组的第一个索引名称:
1
,数组将从 1 开始:<Parent>.1
, <Parent>.2
, <Parent>.3
,...0
,数组将从 0 开始:<Parent>.0
, <Parent>.1
, <Parent>.2
,...""
,数组中的第一项将是未命名的,并且后面跟着 1: <Parent>
, <Parent>.1
, <Parent>.2
,...<强> -ToString <Type[]= [String], [DateTime], [TimeSpan]>
将转换为字符串而不是进一步扁平化的值类型列表(默认 [String], [DateTime], [TimeSpan]
)。例如。一个[DateTime]
可以使用 Date
等附加属性进行扁平化, Day
, DayOfWeek
等,但会被转换为单个 ( String
) 属性。
注意:
参数-Path
供内部使用,但只能用于前缀属性名称。
回答具体问题:
(Get-Content "PATH_TO\test.json" -Raw | ConvertFrom-Json) | Flatten-Object | Convertto-CSV -NoTypeInformation | Set-Content "PATH_TO\test.csv"
结果:
{
"url": "http://test.test",
"slug": "slug",
"id": 10011,
"link": "http://test.er",
"level": 1,
"areas.2": "area_b",
"areas.1": "area_a",
"disciplines.3": "discipline_c",
"disciplines.2": "discipline_b",
"disciplines.1": "discipline_a",
"subject": "testing",
"title": "Test procedure",
"email": "test@test.com"
}
对更复杂的自定义对象进行压力测试:
New-Object PSObject @{
String = [String]"Text"
Char = [Char]65
Byte = [Byte]66
Int = [Int]67
Long = [Long]68
Null = $Null
Booleans = $False, $True
Decimal = [Decimal]69
Single = [Single]70
Double = [Double]71
Array = @("One", "Two", @("Three", "Four"), "Five")
HashTable = @{city="New York"; currency="Dollar"; postalCode=10021; Etc = @("Three", "Four", "Five")}
Object = New-Object PSObject -Property @{Name = "One"; Value = 1; Text = @("First", "1st")}
} | Flatten
结果:
Double : 71
Decimal : 69
Long : 68
Array.1 : One
Array.2 : Two
Array.3.1 : Three
Array.3.2 : Four
Array.4 : Five
Object.Name : One
Object.Value : 1
Object.Text.1 : First
Object.Text.2 : 1st
Int : 67
Byte : 66
HashTable.postalCode : 10021
HashTable.currency : Dollar
HashTable.Etc.1 : Three
HashTable.Etc.2 : Four
HashTable.Etc.3 : Five
HashTable.city : New York
Booleans.1 : False
Booleans.2 : True
String : Text
Char : A
Single : 70
Null :
展平分组对象:
$csv | Group Name | Flatten | Format-Table
# https://stackoverflow.com/a/47409634/1701026
展平常见对象:
(Get-Process)[0] | Flatten-Object
或者对象列表(数组):
Get-Service | Flatten-Object -Depth 3 | Export-CSV Service.csv
请注意,如下命令需要数小时才能计算:
Get-Process | Flatten-Object | Export-CSV Process.csv
为什么?因为它会生成一个包含数百行和数千列的表。因此,如果您想将其用于展平过程,最好限制行数(使用 Where-Object
cmdlet)或列数(使用 Select-Object
cmdlet)。
最新Flatten-Object
版本,请参阅:https://powersnippets.com/flatten-object/
关于json - 将嵌套 JSON 数组转换为 CSV 文件中的单独列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45829754/
Java 专家需要您的帮助。 今天我在一次采访中被问到这个问题,但我无法解决。所以我需要一个解决方案来解决这个问题; 反转字符串 Input : Hello, World! Output : oll
目标:单击按钮并将成分作为单独的项目添加到数组中。 当前设置: 这给出:蓝莓芒果柠檬汁 然后我希望能够通过单击按钮将成分作为单独的项目添加到数组中: var allI
如何编写正则表达式来匹配它(参见箭头): "this is a ->'' this is a "test' there is another "test' 第二种情况 /\b'/ Regex Demo
我有一个数组,其中包含有限数量的项目。我想随机删除项目,直到所有项目都被使用过一次。 示例 [1,2,3,4,5] 使用了随机数 5,所以我不想再这样了。使用了随机数 2,所以我不想再这样了。等等..
首先,抱歉,如果这太主观了,我只是不知道还能怎么问/去哪里问。 无论如何,鉴于我最近的所有问题,我准备很快发布一个 Android 应用程序,并且大部分测试都是在我的手机 Droid 上完成的。我真的
这可能不是这个问题的正确位置,如果不合适请随意移动它。我标记为 Delphi/Pascal 因为这是我在 atm 中工作的内容,但这可能适用于我猜的所有编程。 无论如何,我正在做一些代码清理,并考虑将
我像这样分隔了其余 api 的路由。有没有更好的方法来组织路由器?还是我现在的做法没问题? app.js app.use('/api/auth',auth); 应用程序/ Controller /au
我在 2 个单独的工作表中包含以下数据: 表1: A B C D a ff dd ff ee b 12 10 10 12 表2: A B C
我正在使用 jQuery,并在位于单独 HTML 文件中的表中获取了几行。单击时,每一行都会成功重定向到本地 HTML 文件。 (使用window.location) 我想要实现的目标 我想要完成的是
我有重叠背景图像的问题,当它们重叠时会导致阴影比不重叠时更暗,从而产生不均匀的阴影。 我有一个高度灵活的盒子,带有一些透明的背景图像和阴影以创建漂亮的边框。盒子本质上是 3 个元素。 您可以在此处找到
按照正常的微服务框架,我们希望将每个微服务放入其自己的 git 存储库中,然后为 Service Fabric 项目创建一个存储库。当我们更新其中一个微服务时,Service Fabric 项目将仅重
我想将多个片段嵌入到一个指令中。这是我的设置方式。 Everyone Development (3)
我希望在保留原件的同时将多个文件 gzip 到一个目录中(到多个 .gz 文件中)。 我可以使用这些命令来处理单个文件: find . -type f -name "*cache.html" -exe
有没有办法分别知道每个 Eclipse 插件消耗了多少内存? 最佳答案 进行堆转储并使用例如分析它Eclipse Memory Analyser . 如需更多信息,请参阅 Analyzing Equi
我们使用cusrom插件并以这种方式定义脚本(这是一个近似的伪代码): //It is common part for every script (1) environments { "env1"
我在控制台应用程序中托管了一个集线器,并有一个 WPF 应用程序连接到它。它工作得很好。然后我将集线器移到一个单独的项目中,并将主机的引用添加到新项目中。现在我收到 500 错误,没有其他详细信息。
是否可以在单独的 JAR 文件中为 JavaBean 构建类?具体来说,JavaBean 在一个 JAR 文件中具有 Bean 和 BeanInfo 类,而自定义属性编辑器类位于另一个 JAR 文件中
好的,所以我有一个 MAF 应用程序,它在单独的应用程序域中加载每个插件。这非常适合我的需要,因为它允许我在运行时动态卸载和重新加载我的插件。 问题是,我需要能够在子应用域中处理未处理的异常,捕获它,
在参加在线数据库类(class)(针对初学者)时,我注意到一个问题,我必须查找涉及...至少两个不同值的查询...例如, ELMASRI 书中的 COMPANY 数据库指出:查找至少从事两个不同项目的
(首先:我已经尝试了涉及边距、边框等的所有选项。) Link to problematic page. Link to similarly constructed, non-problematic p
我是一名优秀的程序员,十分优秀!