我有一个数组,其中包含一个包含数据的嵌套哈希表:
tid : 1
token : 12345
participant_info : @{firstname=bob; lastname=smith; email=bob.smith@email.com}
completed : y
tid : 2
token : 67890
participant_info : @{firstname=Alice; lastname=Jones; email=alice.jones@email.com}
completed : n
我想将此输出为CSV,其中第一个名称,姓氏和电子邮件的嵌套项目从内部哈希表中拉出 - 例如
tid,token,firstname,surname,email,completed
1,12345,bob,smith,bob.smith@email.com,y
2,67890,alice,jones,alice.jones@email.com,n
我猜测答案是循环使用foreach并创建一个自定义的ps对象,但由于我的嵌套项目没有命名,我无法弄清楚如何使用此处的其他示例执行此操作。
感谢任何帮助!谢谢!
答案 0 :(得分:3)
鉴于你的样本:
private val searcher = Searcher.create(APP_ID, API_KEY, INDEX_OBJECT)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_search)
setSupportActionBar(tbSearch)
supportActionBar?.setHomeAsUpIndicator(R.drawable.ic_arrow_back_normal_48px)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
val q = Query()
q.filters = "id:Hj5RFIzLjk5XTJOdODeu"
searcher.query = q
val helper = InstantSearch(this, searcher)
helper.search()
}
期望的输出:
@(
[pscustomobject]@{
tid = 1
token = 12345
participant_info = @{
firstname = 'bob'
lastname = 'smith'
email = 'bob.smith@email.com'
}
completed = 'N'
}
...
)
您可以这样做:
id,token,firstname,surname,email,completed
1,12345,bob,smith,bob.smith@email.com,y
2,67890,alice,jones,alice.jones@email.com,n
编辑:您正在处理$JsonList = @( ... )
$Path = "$Env:UserProfile\file.csv"
ForEach ($Sample in $JsonList)
{
$Sample | Select-Object -Property @(
@{N = 'id'; E = { $Sample.tid }}
@{N = 'token'; E = { $Sample.token }}
@{N = 'firstname'; E = { $Sample.participant_info.firstname }}
@{N = 'surname'; E = { $Sample.participant_info.lastname }}
@{N = 'email'; E = { $Sample.participant_info.email }}
@{N = 'completed'; E = { $Sample.completed }}
) | Export-Csv -Path $Path -Append
}
,而不是PSCustomObject
,因此我之前的语法不适用于此。这就是我现在假设您的代码看起来像现在(我已经更新了上面的示例):
Hashtable