Json 意想不到的角色 
Json unexpected character 
我使用以下代码通过 PowerShell 重新生成我的 Json,但是它在 json { 标签开始之前创建了意外字符。
<#$data = Get-Content -Raw -Path myfile.json | ConvertFrom-Json#>
$data = @"
{
"stations": [
{
"code": "1",
"name": "United force"
},
{
"code": "2",
"name": "Toowoon Bay Service Station"
}
],
"prices": [
{
"stationcode": "1",
"fueltype": "DL",
"price": 126.97
},
{
"stationcode": "1",
"fueltype": "E10",
"price": 118.92
},
{
"stationcode": "2",
"fueltype": "E10",
"price": 112.90
},
{
"stationcode": "2",
"fueltype": "P95",
"price": 125.90
},
{
"stationcode": "2",
"fueltype": "P98",
"price": 155.90
},
{
"stationcode": "2",
"fueltype": "U91",
"price": 115.20
}
]
}
"@ | ConvertFrom-Json
foreach ($price in $data.prices) {
$data.stations |
Where-Object { $_.code -eq $price.stationcode } |
Add-Member -MemberType NoteProperty -Name $price.fueltype -Value $price.price
}
$data | Select-Object -Property stations | ConvertTo-Json | Set-Content "testJson.json" -Encoding UTF8
上传后调用此文件时,由于出现意外字符,我无法读取 json 文件。
希望有人可以在 PowerShell 代码中提供帮助,以确保没有这样的字符,我可能正在做一些简单但不确定如何做的事情。
我正在读取 PowerShell 到 Flutter 后生成的 json 文件,它会抛出如下所示的错误
谢谢,
您需要找到一种方法让 Flutter 处理 UTF-8 字节顺序标记 (0xEF 0xBB 0xBF),或者将文件写入没有 BOM 的 UTF8。
如果您使用的是 PowerShell 6 或更高版本,则可以使用
Set-Content "testJson.json" -Encoding utf8NoBOM
对于版本 6 以下的 PowerShell,该版本不可用,因此您可以使用
$json = $data | Select-Object -Property stations | ConvertTo-Json
# [System.IO.File]::WriteAllText() defaults to UTF8 without BOM
# use an absolute path here
[System.IO.File]::WriteAllText("D:\Test\testJson.json", $json)
Windows PowerShell 和 PowerShell Core 都有 -Encoding
Get-Content
和 Set-Content
的参数。
PS C:\> $PSVersionTable.PSVersion.ToString()
5.1.19041.1
ASCII: Uses the encoding for the ASCII (7-bit) character set.
BigEndianUnicode: Encodes in UTF-16 format using the big-endian byte order.
Byte: Encodes a set of characters into a sequence of bytes.
String: Uses the encoding type for a string.
Unicode: Encodes in UTF-16 format using the little-endian byte order.
UTF7: Encodes in UTF-7 format.
UTF8: Encodes in UTF-8 format.
Unknown: The encoding type is unknown or invalid. The data can be treated as binary.
PS C:\> $PSVersionTable.PSVersion.ToString()
7.1.0-preview.3
`ascii`: Uses the encoding for the ASCII (7-bit) character set.
`bigendianunicode`: Encodes in UTF-16 format using the big-endian byte order.
`oem`: Uses the default encoding for MS-DOS and console programs.
`unicode`: Encodes in UTF-16 format using the little-endian byte order.
`utf7`: Encodes in UTF-7 format.
`utf8`: Encodes in UTF-8 format.
`utf8BOM`: Encodes in UTF-8 format with Byte Order Mark (BOM)
`utf8NoBOM`: Encodes in UTF-8 format without Byte Order Mark (BOM)
`utf32`: Encodes in UTF-32 format.
我使用以下代码通过 PowerShell 重新生成我的 Json,但是它在 json { 标签开始之前创建了意外字符。
<#$data = Get-Content -Raw -Path myfile.json | ConvertFrom-Json#>
$data = @"
{
"stations": [
{
"code": "1",
"name": "United force"
},
{
"code": "2",
"name": "Toowoon Bay Service Station"
}
],
"prices": [
{
"stationcode": "1",
"fueltype": "DL",
"price": 126.97
},
{
"stationcode": "1",
"fueltype": "E10",
"price": 118.92
},
{
"stationcode": "2",
"fueltype": "E10",
"price": 112.90
},
{
"stationcode": "2",
"fueltype": "P95",
"price": 125.90
},
{
"stationcode": "2",
"fueltype": "P98",
"price": 155.90
},
{
"stationcode": "2",
"fueltype": "U91",
"price": 115.20
}
]
}
"@ | ConvertFrom-Json
foreach ($price in $data.prices) {
$data.stations |
Where-Object { $_.code -eq $price.stationcode } |
Add-Member -MemberType NoteProperty -Name $price.fueltype -Value $price.price
}
$data | Select-Object -Property stations | ConvertTo-Json | Set-Content "testJson.json" -Encoding UTF8
上传后调用此文件时,由于出现意外字符,我无法读取 json 文件。
希望有人可以在 PowerShell 代码中提供帮助,以确保没有这样的字符,我可能正在做一些简单但不确定如何做的事情。
我正在读取 PowerShell 到 Flutter 后生成的 json 文件,它会抛出如下所示的错误
谢谢,
您需要找到一种方法让 Flutter 处理 UTF-8 字节顺序标记 (0xEF 0xBB 0xBF),或者将文件写入没有 BOM 的 UTF8。
如果您使用的是 PowerShell 6 或更高版本,则可以使用
Set-Content "testJson.json" -Encoding utf8NoBOM
对于版本 6 以下的 PowerShell,该版本不可用,因此您可以使用
$json = $data | Select-Object -Property stations | ConvertTo-Json
# [System.IO.File]::WriteAllText() defaults to UTF8 without BOM
# use an absolute path here
[System.IO.File]::WriteAllText("D:\Test\testJson.json", $json)
Windows PowerShell 和 PowerShell Core 都有 -Encoding
Get-Content
和 Set-Content
的参数。
PS C:\> $PSVersionTable.PSVersion.ToString()
5.1.19041.1
ASCII: Uses the encoding for the ASCII (7-bit) character set.
BigEndianUnicode: Encodes in UTF-16 format using the big-endian byte order.
Byte: Encodes a set of characters into a sequence of bytes.
String: Uses the encoding type for a string.
Unicode: Encodes in UTF-16 format using the little-endian byte order.
UTF7: Encodes in UTF-7 format.
UTF8: Encodes in UTF-8 format.
Unknown: The encoding type is unknown or invalid. The data can be treated as binary.
PS C:\> $PSVersionTable.PSVersion.ToString()
7.1.0-preview.3
`ascii`: Uses the encoding for the ASCII (7-bit) character set.
`bigendianunicode`: Encodes in UTF-16 format using the big-endian byte order.
`oem`: Uses the default encoding for MS-DOS and console programs.
`unicode`: Encodes in UTF-16 format using the little-endian byte order.
`utf7`: Encodes in UTF-7 format.
`utf8`: Encodes in UTF-8 format.
`utf8BOM`: Encodes in UTF-8 format with Byte Order Mark (BOM)
`utf8NoBOM`: Encodes in UTF-8 format without Byte Order Mark (BOM)
`utf32`: Encodes in UTF-32 format.