下面的代码读取文件系统并将JSON对象返回到php页面,然后将其解析出来。
我正在尝试添加一个“Questions”属性,该属性读取csv文件
“$ .FullName +”\“+ $ .Name +”-questions.csv“
并返回一个JSON对象,该对象将嵌套在父对象中。现在它只返回一个“[”。不知道为什么?任何帮助
Param(
[Parameter(Mandatory = $true)][string]$path
)
function Add-Tabstops{
param($Count)
$tabs = ""
for($i=0; $i -lt $Count; $i++){$tabs += " "}
return $tabs
}
function Read-Questions($name){
$name = $name + "-questions.csv"
if(Test-Path($name)){
$questions= Import-CSV $name | ConvertTo-JSON
return "["
$questions
"]"
}
else{
return "None"
}
}
function Process-Path{
param($Path)
if (Test-Path "$path"){
$source = $path.Split("\")
$source = $source[($source.Length -1)]
Output-JsonChildren -Path "$path" -Source $source
}
else {
return '"No Objects Found!"'
}
}
function Output-JsonChildren{
param($Path, $Level = 1, $Source)
return $(Get-ChildItem -Path $Path -Directory | Where-Object{$_} | ForEach-Object{
(Add-Tabstops $Level) +
"{`n" +
(Add-Tabstops ($Level+1)) +
"`"Name`"`: `"$($_.Name)`"," +
"`n" +
(Add-Tabstops ($Level+1)) +
"`"Image`"`: `"$($_.Name)`"," +
"`n" +
(Add-Tabstops ($Level+1)) +
"`"displayName`"`: `"$((Get-Content($($_.FullName + "\" + $_.Name + ".txt"))).split('-')[0])`"," +
"`n" +
(Add-Tabstops ($Level+1)) +
"`"Attribute1`"`: `"$((Get-Content($($_.FullName + "\" + $_.Name + ".txt"))).split('-')[1])`"," +
"`n" +
(Add-Tabstops ($Level+1)) +
"`"Attribute2`"`: `"$((Get-Content($($_.FullName + "\" + $_.Name + ".txt"))).split('-')[2])`"," +
"`n" +
(Add-Tabstops ($Level+1)) +
"`"Attribute3`"`: `"$((Get-Content($($_.FullName + "\" + $_.Name + ".txt"))).split('-')[3])`"," +
"`n" +
(Add-Tabstops ($Level+1)) +
"`"Attribute4`"`: `"$Source`"," +
"`n" +
(Add-Tabstops ($Level+1)) +
"`"Questions`"`: `"$(Read-Questions($($_.FullName + "\" + $_.Name)))`"," +
"`n" +
(Add-Tabstops ($Level+1)) +
"`"children`": ["+
$(if($_.psiscontainer){"`n" + (Output-JsonChildren -Path $_.FullName -Level ($Level+2))+ "`n" + (Add-Tabstops ($Level+1))}) +
"]`n" +
(Add-Tabstops ($Level)) +
"}"
}) -join ",`n"
}
$JSON = Process-Path -Path $path
"["
$JSON
"]"
答案 0 :(得分:1)
您的代码:
return "["
$questions
"]"
让我们将这三行和一些分号重新格式化为显式行结尾,但保持功能相同:
return "[";
$questions;
"]";
这是否清楚说明为什么函数总是返回[
?
你想:
return '[' + $Questions.ToString() + ']';