Json unexpected character 

情到浓时终转凉″ 提交于 2021-01-28 19:26:19

问题


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

When calling this file after uploading, I am having trouble reading the json file due to that unexpected character.

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.

I am reading the generated json file after the PowerShell to Flutter, which throws the error as shown below

Thanks,


回答1:


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.

If you're using PowerShell 6 or up, you can use

Set-Content "testJson.json" -Encoding utf8NoBOM

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)



回答2:


Both Windows PowerShell and PowerShell Core have -Encoding parameters for Get-Content and 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.


来源:https://stackoverflow.com/questions/63969957/json-unexpected-character-%c3%af

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!