ConvertFrom-Json :无法转换 JSON 字符串,因为从字符串转换而来的字典包含重复的键 [英] ConvertFrom-Json : Cannot convert the JSON string because a dictionary that was converted from the string contains the duplicated keys

查看:112
本文介绍了ConvertFrom-Json :无法转换 JSON 字符串,因为从字符串转换而来的字典包含重复的键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OData API 服务返回以下 JSON:

The following JSON is getting returned from OData API service:

{
  "d": {
    "results": [
      {
        "FileSystemObjectType": 0,
        "Id": 1,
        "ContentTypeId": "0x0100BC97B2F575CB0C42B79549F3BABD32A8",
        "Title": "Nokia California",
        "Address": "200 South Matilda Avenue\nW Washington Ave\n94086 Sunnyvale, California\nUnited States of America",
        "ID": 1,
        "Modified": "2014-02-24T10:06:39Z",
        "Created": "2014-02-24T10:06:39Z",
        "AuthorId": 12,
        "EditorId": 12,
        "OData__UIVersionString": "1.0",
        "Attachments": false,
        "GUID": "d12aafad-502a-4968-a69e-36a7ea05ec80"
      }
    ]
  }
}

并作为字符串保存到名为 $data

and saved as a string into variable named $data

尝试使用 ConvertFrom 将 JSON 格式的字符串转换为自定义对象-Json cmdlet:

An attempt to convert a JSON-formatted string to a custom object using ConvertFrom-Json cmdlet:

$results = $data | ConvertFrom-Json

给出以下错误:

ConvertFrom-Json:无法转换 JSON 字符串,因为字典从字符串转换而来的包含重复的键Id"和ID".

ConvertFrom-Json : Cannot convert the JSON string because a dictionary that was converted from the string contains the duplicated keys 'Id' and 'ID'.

有没有办法在 PowerShell 中转换指定的 JSON 格式的字符串?

Is there any way to convert the specified JSON-formatted string in PowerShell?

推荐答案

在 PowerShell V1.0 中,或者在 PowerShell V2.0 中当 JSON 太大时,我仍然使用转换为 XML :

In PowerShell V1.0, or in PowerShell V2.0 when the JSON is too big, I still use a convertion to XML :

Add-Type -AssemblyName System.ServiceModel.Web, System.Runtime.Serialization
function Convert-JsonToXml
{
  PARAM([Parameter(ValueFromPipeline=$true)][string[]]$json)

  BEGIN
  { 
    $mStream = New-Object System.IO.MemoryStream 
  }

  PROCESS
  {
    $json | Write-String -stream $mStream
  }

  END
  {
    $mStream.Position = 0
    try
    {
       $jsonReader = [System.Runtime.Serialization.Json.JsonReaderWriterFactory]::CreateJsonReader($mStream,[System.Xml.XmlDictionaryReaderQuotas]::Max)
       $xml = New-Object Xml.XmlDocument
       $xml.Load($jsonReader)
       $xml
    }
    finally
    {
       $jsonReader.Close()
       $mStream.Dispose()
    }
  }
}

使用此代码,您可以遍历您可以测试的项目:

Using this code you can loop thru your items you can test :

$a = Get-Content C:\temp\jsontest.txt
$b.root.d.results.Item
$b.root.d.results.Item[7].Id[0].InnerText

<小时>

(已编辑)


(Edited)

在你的情况下,我只会替换预期的重复 ID/Id

In you case I would only replace the expected duplicate ID/Id

$data = Get-Content C:\temp\jsontest.txt -Raw
$datacorrected = $a -creplace '"Id":','"Id-minus":'
$psJsonIn = $datacorrected | ConvertFrom-Json

如果你真的有意外的重复,你可以编写一个函数来捕获由于重复的密钥而导致的转换错误并替换一个.

If really you've got unexpected duplicate you can write a function that trap the convertion error due to duplicated key and replace one.

这篇关于ConvertFrom-Json :无法转换 JSON 字符串,因为从字符串转换而来的字典包含重复的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆