杰森出乎意料的角色 [英] Json unexpected character 
问题描述
我正在使用以下代码使用PowerShell重新生成Json,但是它会在json {标签开始之前创建意外的字符.
I am using below code to regenerate my Json using PowerShell, however it creates unexpected character before the start of the json { tag.
<#$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文件.
When calling this file after uploading, I am having trouble reading the json file due to that unexpected character.
希望有人可以在PowerShell代码中提供帮助,以确保没有这样的字符,我可能正在做一些简单的事情,但不确定如何做.
Hope someone can help in the PowerShell code to ensure there is no character like that, I may be doing something easy but not sure how.
我正在从PowerShell到Flutter之后读取生成的json文件,这将引发错误,如下所示
I am reading the generated json file after the PowerShell to Flutter, which throws the error as shown below
谢谢
推荐答案
您需要找到一种方法来让Flutter处理UTF-8字节顺序标记(0xEF 0xBB 0xBF),或者将文件写为UTF8,而无需BOM
You need to find a way to have Flutter deal with the UTF-8 Byte Order Mark (0xEF 0xBB 0xBF), OR write the file as UTF8 without the BOM.
如果您使用的是PowerShell 6或更高版本,则可以使用
If you're using PowerShell 6 or up, you can use
Set-Content "testJson.json" -Encoding utf8NoBOM
对于低于版本6的PowerShell,它不可用,因此您可以使用
For PowerShell below version 6, that is not available, so you can use
$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)
这篇关于杰森出乎意料的角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!