使用 PowerShell 将 JSON 转换为 plist

问题描述

将json转plist有很多方法,最方便的就是用苹果系统下的命令,或者用网上的生成工具,但是PowerShell不提供直接命令,我在GitHub上找了个项目,可以方便的转换对于简单的JSON,但是对于一些稍微复杂的JSON,转换后会出现一些问题

项目地址:https://github.com/msftrncs/PwshJSONtoPList

现有问题:

某些数组没有正确转换

我录制了一个视频演示https://www57.zippyshare.com/v/krihd4Y8/file.html

相关文件https://www57.zippyshare.com/v/84wYYTRx/file.html

powershell 脚本代码

. '.\ConvertTo-PList.ps1' # Put ConvertTo-PList.ps1 in the same directory
Get-Content "test.json" | ConvertFrom-Json |
ConvertTo-Plist -Indent "`t" -StateEncodingAs 'UTF-8' |
Set-Content 'Generated.plist' -Encoding 'UTF8'

test.json:

{
  "ACPI": {
    "DSDT": {
      "Debug": false,"DropOEM_DSM": false,"Fixes": {
        "AddDTGP": false,"FixHPET": false,"FixRTC": false,"FixShutdown": false
      },"Patches": [
        {
          "Comment": "change SAT0 to SATA","disabled": false,"Find": "U0FUMA==","Replace": "U0FUQQ=="
        },{
          "Comment": "change HECI to IMEI","Find": "SEVDSQ==","Replace": "SU1FSQ=="
        }
      ],"ReuseFFFF": false
    },"DropTables": [
      {
        "Signature": "DMAR"
      },{
        "Signature": "MATS"
      }
    ],"FixHeaders": true,"SSDT": {
      "DropOem": false,"Generate": {
        "CStates": true,"PStates": true
      }
    }
  },"GUI": {
    "Custom": {
      "Entries": [
        {
          "disabled": false,"FullTitle": "MAC OS X","Hidden": false,"InjectKexts": true,"NoCaches": true,"Type": "OSX","Volume": "2D4A5E3A-04FC-3041-471B-8A1622089D19"
        },{
          "disabled": false,"FullTitle": "WIN","Type": "Windows","Volume": "2BDB20A2-1E67-4AE2-753D-D85E9A410000"
        }
      ]
    },"Hide": [
      "Preboot","Recovery","BOOTX64.EFI","DATA"
    ]
  },"KernelAndKextPatches": {
    "ForceKextsToLoad": [
      "\\System\\Library\\Extensions\\IONetworkingFamily.kext"
    ],"KextsToPatch": [
      {
        "Comment": "External icons patch","Find": "RXh0ZXJuYWw=","InfoPlistPatch": false,"Name": "AppleAHCIPort","Replace": "SW50ZXJuYWw="
      },{
        "Comment": "Enable TRIM for SSD","Find": "AEFQUExFIFNTRAA=","Name": "com.apple.iokit.IOAHCIBlockStorage","Replace": "AAAAAAAAAAAAAAA="
      }
    ]
  }
}

Generated.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>ACPI</key>
    <dict>
        <key>DSDT</key>
        <dict>
            <key>Debug</key>
            <false/>
            <key>DropOEM_DSM</key>
            <false/>
            <key>Fixes</key>
            <string>@{AddDTGP=False; FixHPET=False; FixRTC=False; FixShutdown=False}</string>
            <key>Patches</key>
            <string> </string>
            <key>ReuseFFFF</key>
            <false/>
        </dict>
        <key>DropTables</key>
        <array>
            <string>@{Signature=DMAR}</string>
            <string>@{Signature=MATS}</string>
        </array>
        <key>FixHeaders</key>
        <true/>
        <key>SSDT</key>
        <dict>
            <key>DropOem</key>
            <false/>
            <key>Generate</key>
            <string>@{CStates=True; PStates=True}</string>
        </dict>
    </dict>
    <key>GUI</key>
    <dict>
        <key>Custom</key>
        <dict>
            <key>Entries</key>
            <string> </string>
        </dict>
        <key>Hide</key>
        <array>
            <string>Preboot</string>
            <string>Recovery</string>
            <string>BOOTX64.EFI</string>
            <string>DATA</string>
        </array>
    </dict>
    <key>KernelAndKextPatches</key>
    <dict>
        <key>ForceKextsToLoad</key>
        <array>
            <string>\System\Library\Extensions\IONetworkingFamily.kext</string>
        </array>
        <key>KextsToPatch</key>
        <array>
            <string>@{Comment=External icons patch; disabled=False; Find=RXh0ZXJuYWw=; InfoPlistPatch=False; Name=AppleAHCIPort; Replace=SW50ZXJuYWw=}</string>
            <string>@{Comment=Enable TRIM for SSD; disabled=False; Find=AEFQUExFIFNTRAA=; InfoPlistPatch=False; Name=com.apple.iokit.IOAHCIBlockStorage; Replace=AAAAAAAAAAAAAAA=}</string>
        </array>
    </dict>
</dict>
</plist>

错误图片

enter image description here

解决方法

我看到的版本 here 也有一个 -Depth 参数。

如果我在您的 json 上使用它并将深度设置为 4(或更高)

$json | ConvertFrom-Json | ConvertTo-Plist -Indent "`t" -StateEncodingAs 'UTF-8' -Depth 4

它产生这个:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>ACPI</key>
    <dict>
        <key>DSDT</key>
        <dict>
            <key>Debug</key>
            <false/>
            <key>DropOEM_DSM</key>
            <false/>
            <key>Fixes</key>
            <dict>
                <key>AddDTGP</key>
                <false/>
                <key>FixHPET</key>
                <false/>
                <key>FixRTC</key>
                <false/>
                <key>FixShutdown</key>
                <false/>
            </dict>
            <key>Patches</key>
            <array>
                <dict>
                    <key>Comment</key>
                    <string>change SAT0 to SATA</string>
                    <key>Disabled</key>
                    <false/>
                    <key>Find</key>
                    <string>U0FUMA==</string>
                    <key>Replace</key>
                    <string>U0FUQQ==</string>
                </dict>
                <dict>
                    <key>Comment</key>
                    <string>change HECI to IMEI</string>
                    <key>Disabled</key>
                    <false/>
                    <key>Find</key>
                    <string>SEVDSQ==</string>
                    <key>Replace</key>
                    <string>SU1FSQ==</string>
                </dict>
            </array>
            <key>ReuseFFFF</key>
            <false/>
        </dict>
        <key>DropTables</key>
        <array>
            <dict>
                <key>Signature</key>
                <string>DMAR</string>
            </dict>
            <dict>
                <key>Signature</key>
                <string>MATS</string>
            </dict>
        </array>
        <key>FixHeaders</key>
        <true/>
        <key>SSDT</key>
        <dict>
            <key>DropOem</key>
            <false/>
            <key>Generate</key>
            <dict>
                <key>CStates</key>
                <true/>
                <key>PStates</key>
                <true/>
            </dict>
        </dict>
    </dict>
    <key>GUI</key>
    <dict>
        <key>Custom</key>
        <dict>
            <key>Entries</key>
            <array>
                <dict>
                    <key>Disabled</key>
                    <false/>
                    <key>FullTitle</key>
                    <string>MAC OS X</string>
                    <key>Hidden</key>
                    <false/>
                    <key>InjectKexts</key>
                    <true/>
                    <key>NoCaches</key>
                    <true/>
                    <key>Type</key>
                    <string>OSX</string>
                    <key>Volume</key>
                    <string>2D4A5E3A-04FC-3041-471B-8A1622089D19</string>
                </dict>
                <dict>
                    <key>Disabled</key>
                    <false/>
                    <key>FullTitle</key>
                    <string>WIN</string>
                    <key>Hidden</key>
                    <false/>
                    <key>Type</key>
                    <string>Windows</string>
                    <key>Volume</key>
                    <string>2BDB20A2-1E67-4AE2-753D-D85E9A410000</string>
                </dict>
            </array>
        </dict>
        <key>Hide</key>
        <array>
            <string>Preboot</string>
            <string>Recovery</string>
            <string>BOOTX64.EFI</string>
            <string>DATA</string>
        </array>
    </dict>
    <key>KernelAndKextPatches</key>
    <dict>
        <key>ForceKextsToLoad</key>
        <array>
            <string>\System\Library\Extensions\IONetworkingFamily.kext</string>
        </array>
        <key>KextsToPatch</key>
        <array>
            <dict>
                <key>Comment</key>
                <string>External icons patch</string>
                <key>Disabled</key>
                <false/>
                <key>Find</key>
                <string>RXh0ZXJuYWw=</string>
                <key>InfoPlistPatch</key>
                <false/>
                <key>Name</key>
                <string>AppleAHCIPort</string>
                <key>Replace</key>
                <string>SW50ZXJuYWw=</string>
            </dict>
            <dict>
                <key>Comment</key>
                <string>Enable TRIM for SSD</string>
                <key>Disabled</key>
                <false/>
                <key>Find</key>
                <string>AEFQUExFIFNTRAA=</string>
                <key>InfoPlistPatch</key>
                <false/>
                <key>Name</key>
                <string>com.apple.iokit.IOAHCIBlockStorage</string>
                <key>Replace</key>
                <string>AAAAAAAAAAAAAAA=</string>
            </dict>
        </array>
    </dict>
</dict>
</plist>

显然你有一个旧版本的脚本。