错误格式错误的UTF-8字符,可能编码错误

时间:2017-01-24 12:09:08

标签: json excel powershell

我有一个从Excel文件中读取的PowerShell脚本。它存储来自特定单元格/列的数据,转换为JSON,然后通过REST将其发送到我的Wordpress安装。

我遇到的问题是,当尝试使用从Excel中获取的数据运行脚本时,它会显示错误

  

格式错误的UTF-8字符,可能编码错误

#Declare the file path and sheet name
$file = "P:\file.xlsx"
$sheetName = "IN PRODUCTION"

###############################################
#                                             #
#             EXCEL FUNCTIONS                 #
#                                             #
###############################################

#Create an instance of Excel.Application and Open Excel file
$objExcel = New-Object -ComObject Excel.Application
$workbook = $objExcel.Workbooks.Open($file)
$sheet = $workbook.Worksheets.Item($sheetName)
$objExcel.Visible = $false

#Count max row
$rowMax = ($sheet.UsedRange.Rows).count 

#Declare the starting positions
$rowName,$colName = 1,1
$rowSignOff,$colSignOff = 1,2
$rowReceived,$colReceived = 1,3
$rowBuildStart,$colBuildStart = 1,4
$rowBuildEnd,$colBuildEnd = 1,5
$rowShipping,$colShipping = 1,6
$rowBuiltBy,$colBuiltBy = 1,7
$rowQA,$colQA = 1,8
$rowCage,$colCage = 1,9
$rowComment,$colComment = 1,10
$rowStatus,$colStatus = 1,11

$build = @()

#Loop to get values and store it
for ($i=1; $i -le $rowMax-1; $i++)
{
    $name = $sheet.Cells.Item($rowName+$i, $colName).Text
    $signoff = $sheet.Cells.Item($rowSignOff+$i, $colSignOff).Text
    $received = $sheet.Cells.Item($rowReceived+$i, $colReceived).Text
    $buildstart = $sheet.Cells.Item($rowBuildStart+$i, $colBuildStart).Text
    $buildend = $sheet.Cells.Item($rowBuildEnd+$i, $colBuildEnd).Text
    $shipping = $sheet.Cells.Item($rowShipping+$i, $colShipping).Text
    $builtby = $sheet.Cells.Item($rowBuiltBy+$i, $colBuiltBy).Text
    $qa = $sheet.Cells.Item($rowQA+$i, $colQA).Text
    $cage = $sheet.Cells.Item($rowCage+$i, $colCage).Text
    $comment = $sheet.Cells.Item($rowComment+$i, $colComment).Text
    $status = $sheet.Cells.Item($rowStatus+$i, $colStatus).Text

    $build += [PSCustomObject]@{
        name = $name
        start = $buildstart
        end = $buildend
        by = $builtby
        notes = $comment
    }
}

###############################################
#                                             #
#              POST FUNCTIONS                 #
#                                             #
###############################################

$content = [PSCustomObject]@{
    staging_fields = @{
        staging_repeater=$build
    }
}


$json = $content | ConvertTo-Json -Depth $([int32]::MaxValue)
Invoke-RestMethod -Uri $uri -Method POST -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -ContentType "application/json" -Body $json
Write-Host $json

#Close excel file
$objExcel.Quit()

Write-Host $json的输出如下

{
    "staging_fields": {
        "staging_repeater": [
            {
                "name": "Test 1",
                "start": "19/12/2016",
                "end": "09/01/2017",
                "by": "JM",
                "notes": ""
            },
            {
                "name": "Test 2",
                "start": "01/01/2017",
                "end": "11/01/2017",
                "by": "JC",
                "notes": ""
            },
            {
                "name": "Test 3",
                "start": "17/01/2017",
                "end": "01/02/2017",
                "by": "JM",
                "notes": ""
            }
        ]
    }
}

将其粘贴到邮递员并发送POST请求不会产生任何错误,并成功添加到我的WordPress网站。

如果有帮助,则完整错误低于

Invoke-RestMethod : {"code":"rest_invalid_json","message":"Invalid JSON body passed.","data":{"status":400,"json_error_code":5,"json_error_message":"Malformed UTF-8 
characters, possibly incorrectly encoded"}}
At L:\\Untitled1.ps1:98 char:1
+ Invoke-RestMethod -Uri $uri -Method POST -Headers @{Authorization=("B ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

如果我用“Test”等字符串替换变量,脚本可以正常工作。以下是一个例子。

#Loop to get values and store it
for ($i=1; $i -le $rowMax-1; $i++)
{
    $name = $sheet.Cells.Item($rowName+$i, $colName).Text 
    $signoff = $sheet.Cells.Item($rowSignOff+$i, $colSignOff).Text
    $received = $sheet.Cells.Item($rowReceived+$i, $colReceived).Text
    $buildstart = $sheet.Cells.Item($rowBuildStart+$i, $colBuildStart).Text
    $buildend = $sheet.Cells.Item($rowBuildEnd+$i, $colBuildEnd).Text
    $shipping = $sheet.Cells.Item($rowShipping+$i, $colShipping).Text
    $builtby = $sheet.Cells.Item($rowBuiltBy+$i, $colBuiltBy).Text
    $qa = $sheet.Cells.Item($rowQA+$i, $colQA).Text
    $cage = $sheet.Cells.Item($rowCage+$i, $colCage).Text
    $comment = $sheet.Cells.Item($rowComment+$i, $colComment).Text
    $status = $sheet.Cells.Item($rowStatus+$i, $colStatus).Text

    $build += [PSCustomObject]@{
        name = "test"
        start = "test"
        end = "test"
        by = "test"
        notes = "test"
    }
}

来自Excel的数据似乎没有使用正确的字符编码。这是我不知所措的地方。

1 个答案:

答案 0 :(得分:1)

我是个白痴。

在excel文件中,自动更正已将 cafe 更改为Café,这是问题的原因。简单地重命名就解决了这个问题。