[英]Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value:
[英]Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value: [.
我第一次做 C# JSON <-> PHP JSON。 以為我會走上一條輕松的道路,但似乎我已經觸礁了。
我相當確定來自 Newtonsoft 的 JSON 允許“ [ ”字符,但不知道為什么我有這個錯誤?
這是我的 C# 代碼:
public class SystemJSON
{
public bool Status { get; set; }
public string Message { get; set; }
public string ResponseData { get; set; }
}
public static class SystemCall
{
public static String Post(string uri, NameValueCollection pairs)
{
byte[] response = null;
using (WebClient wc = new WebClient())
{
response = wc.UploadValues(uri, pairs);
}
return Encoding.Default.GetString(response);
}
}
string system_Response = SystemCall.Post("http://127.0.0.1:8080/edsa-NEFS%20(PHP)/api.php", new NameValueCollection()
{
{"do_work", Functions.Get_Department_List.ToString()},
{"api_data", null }
});
**SystemJSON systemJSON = JsonConvert.DeserializeObject<SystemJSON>(system_Response);** //<-- Error happens here.
if(systemJSON.Status == true)
{
//do stuff here
}else
{
MessageBox.Show(this, systemJSON.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
這是我的 PHP 代碼:
<?php
// Load Request
$function_name = isset($_POST['do_work']) ? $_POST['do_work'] : '';
$api_data = isset($_POST['api_data']) ? $_POST['api_data'] : '';
// Validate Request
if (empty($function_name))
{
SystemResponse(false, 'Invalid Request');
}
if (!function_exists($function_name))
{
SystemResponse(false, 'API Method Not Implemented');
}
// Call API Method
call_user_func($function_name, $api_data);
/* Helper Function */
function SystemResponse($responseStatus, $responseMessage, $responseData = '')
{
exit(json_encode(array(
'Status' => $responseStatus,
'Message' => $responseMessage,
'ResponseData' => $responseData
)));
}
/* API Methods */
function Get_Department_List($api_data)
{
//Test ------------------------------------------START
$node = array();
$dept = array();
$responseData = array();
$dept['id'] = 1;
$dept['name'] = "General";
$dept['description'] = "Forms, Samples, Templates, Catalogs, etc";
$dept['status'] = 1;
array_push($node, $dept);
$dept['id'] = 2;
$dept['name'] = "Test";
$dept['description'] = "Testing";
$dept['status'] = 1;
array_push($node, $dept);
$responseData["dept"] = $dept;
SystemResponse(true, 'SUCCESS', $responseData);
//Test ------------------------------------------END
}
?>
這是我的錯誤:
Newtonsoft.Json.JsonReaderException HResult=0x80131500
消息=解析值時遇到意外字符:{。 路徑“ResponseData”,第 1 行,位置 51。
問題是您的 C# SystemJSON 類與傳入 JSON 的結構不正確匹配。
C# SystemJSON 類中的ResponseData
被列為string
,但您的 PHP 似乎在該屬性內推出了一個復雜對象。 您不能將對象反序列化為字符串 - 反序列化器無法知道如何將對象結構轉換為合適的字符串,無論如何這通常不是有用或合乎邏輯的事情。 因此,它會拋出一個錯誤,說對象結構不匹配。
您看到的特定錯誤意味着反序列化器期望"
表示字符串的開頭,但它看到的是{
表示另一個對象的開頭。
為什么會這樣? 好吧,您的 PHP 代碼將生成如下所示的 JSON 響應:
{
"Status": true,
"Message": "SUCCESS",
"ResponseData": {
"dept": {
"id": 2,
"name": "Test",
"description": "Testing",
"status": 1
}
}
}
如您所見,ResponseData 包含一個對象,該對象具有“部門”,而“部門”又是另一個具有四個屬性的對象。
要正確反序列化,您的 SystemJSON 類需要更改,您還需要兩個子類來幫助它:
public class SystemJSON
{
public bool Status { get; set; }
public string Message { get; set; }
public ResponseData ResponseData { get; set; }
}
public class ResponseData {
public Department dept {get; set; }
}
public class Department {
public string id {get; set; }
public string description {get; set; }
public int status {get; set; }
}
您現在將能夠正確反序列化 JSON。 這是反序列化的現場演示。
PS [
字符在這里似乎無關緊要......不清楚你為什么在你的問題中提到它。
PPS 通過查看您的 PHP,我猜測您可能打算在 ResponseData 中返回不同的數據結構,具體取決於為do_work
指定的參數 - 即取決於調用的 PHP 函數。 如果是這樣,那么您需要相應地修改您的 C#,以便根據它請求的 API 方法反序列化為不同的具體類。 或者您可能會作弊並將ResponseData
指定為dynamic
,然后它將接受它收到的任何數據結構,盡管需要注意的是它現在實際上是松散類型的,因此在編譯代碼時您會失去某些好處,例如檢查屬性的有效使用名稱、數據類型等
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.