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

我有一个从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": "" } ] } } 

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

如果有帮助,完整的错误在下面

 调用RestMethod:{“code”:“rest_invalid_json”,“message”:“无效的JSON主体传递。”,“data”:{“status”:400,“json_error_code”:5,“json_error_message”:“ 8 
字符,可能错误编码“}}
在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”之类的stringreplacevariables,则该脚本正常工作。 下面是一个例子。

 #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的数据没有通过正确的字符编码。 这是我无所适从的地方。

我是个白痴。

在excel文件中,自动更正已经将咖啡 改成咖啡 ,这是造成问题的原因。 简单的重命名为我解决了这个问题。