Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 如何在解析JSON文件之前验证Powershell中的JSON语法_.net_Json_Powershell_Standards - Fatal编程技术网

.net 如何在解析JSON文件之前验证Powershell中的JSON语法

.net 如何在解析JSON文件之前验证Powershell中的JSON语法,.net,json,powershell,standards,.net,Json,Powershell,Standards,在启动解析脚本之前,我试图将文件验证为JSON。我试图捕获一个重复的密钥,或者如果该文件不是有效的JSOn文件。但它似乎不起作用,请提供任何帮助: function ParseFile([string]$file, [int]$domainNumber) { # read entire JSON file and parse $bytes = [system.io.file]::ReadAllText($file) $json = ConvertFrom-Json $b

在启动解析脚本之前,我试图将文件验证为JSON。我试图捕获一个重复的密钥,或者如果该文件不是有效的JSOn文件。但它似乎不起作用,请提供任何帮助:

function ParseFile([string]$file, [int]$domainNumber)
{
    # read entire JSON file and parse

    $bytes = [system.io.file]::ReadAllText($file)
    $json = ConvertFrom-Json $bytes

    $text = Get-Content $file -Raw 

    try {
        $powershellRepresentation = ConvertFrom-Json $text -ErrorAction Stop;       
        $validJson = $true;
        Write-Error "IN TRY";
    } catch {
        Write-Error "IN CATCH";
        $validJson = $false;
    }


    if ($validJson) {
        Write-Error "Provided text has been correctly parsed to JSON";
        Exit 1
    } else {
        Write-Error "Provided text is not a JSON valid string";
        Exit 1
    } 
它总是说文件是有效的JSON,即使文件包含重复的密钥。调用powershell.exe-noprofile-executionpolicy bypass-file D:\Tools\Scripts json\u files\config.pkg.xml ParseFile:111位于D:\Tools\Scripts\json.ps1:110 char:4+ParseFile$file$file$domainNumber+~~~~~~~~~~~~~~~~~~~~~~~~类别信息:未指定:(:)[写入错误],WriteErrorException+FullyQualifiedErrorId:Microsoft.PowerShell.Commands.WriteErrorException,ParseFile

ParseFile:2222位于D:\Tools\Scripts\json.ps1:110字符:4+ParseFile$file$domainNumber+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+CategoryInfo:NotSpecified:(:)[写入错误],WriteErrorException+FullyQualifiedErrorId:Microsoft.PowerShell.Commands.WriteErrorException,ParseFile

ParseFile:提供的文本已在D:\Tools\Scripts\JSON.ps1:110处正确解析为JSON字符:4+ParseFile$file$domainNumber+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~类别信息:未指定:(:)[写入错误],WriteErrorException+FullyQualifiedErrorId:Microsoft.PowerShell.Commands.WriteErrorException,ParseFile


样本:

{
  "sr_version": {
    "major": 1,
    "minor": 1,
    "patch": 1
  },
  "sr_domain": {
    "soc": "msm",
    "domain": "Audio",
    "subdomain": "root",
    "qmi_instance_id": 74
  },
  "sr_domain": {
    "soc": "msm",
    "domain": "Audio",
    "subdomain": "root",
    "qmi_instance_id": 74
  },
  "sr_service": [{
    "provider": "tms",
    "service": "servreg",
    "service_data_valid": 0,
    "service_data": 0
  }]
}
根据回答:两次同一个键在同一级别不一定是错误的

ConvertFrom-Json : Cannot convert the JSON string because a dictionary that was converted from the string contains the duplicated keys 'Key' and 'key'.
At C:\Temp\Untitled8.ps1:27 char:11
+ $b = $a | ConvertFrom-Json
+           ~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [ConvertFrom-Json], InvalidOperationException
    + FullyQualifiedErrorId : DuplicateKeysInJsonString,Microsoft.PowerShell.Commands.ConvertFromJsonCommand
NET反序列化程序(“System.Web.Script.Serialization.JavaScriptSerializer”?)没有将其检测为错误,它只保留第二个值

如果您尝试使用相同的密钥,但区分大小写,则会出现错误

ConvertFrom-Json : Cannot convert the JSON string because a dictionary that was converted from the string contains the duplicated keys 'Key' and 'key'.
At C:\Temp\Untitled8.ps1:27 char:11
+ $b = $a | ConvertFrom-Json
+           ~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [ConvertFrom-Json], InvalidOperationException
    + FullyQualifiedErrorId : DuplicateKeysInJsonString,Microsoft.PowerShell.Commands.ConvertFromJsonCommand

请参阅在powershell中使用JSON.NET验证的示例。我检查了这些示例,但没有任何帮助。我试图在解析开始之前捕获JSON文件中的重复密钥。正在寻找可以检查文件并让我知道它是否是JSON标准的机制。你能给我们看一个JSON示例吗?{“sr_版本”:{“major”:1,“minor”:1,“patch”:1},“sr_域”:{“soc”:“msm”,“domain”:“Audio”,“subdomain”:“root”,“qmi_实例_id”:74},“sr_域”:{“soc”:“msm”,“domain”:“Audio”,“子域”:“根”,“qmi_实例_id”:74},“sr_服务”:[{“提供者”:“tms”,“服务”:“servreg”,“服务数据_有效”:0,“服务数据”:0}]}在上面的JSON文件中,SR_域被重复,这与JSON验证器不一致,但我们是否有任何解决方案可以捕获重复密钥而不区分大小写?我不这么认为,因为重复密钥包含在标准中。