【发布时间】:2020-12-21 14:00:33
【问题描述】:
我的应用程序中有一个脚本,它从我的 PHP REST API 收集一些 JSON 数据。 API 使用json_encode() 序列化它生成的多维数组。我的应用程序中的脚本应该将其反序列化为 C# 中的等价物,以便我可以循环遍历它,因为每个子数组都代表数据库中的一“行”信息。但是,我遇到了这个错误:
Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: A. Path '', line 0, position 0.
at Newtonsoft.Json.JsonTextReader.ParseValue () [0x002b3] in <2073514815234917a5e8f91b0b239405>:0
at Newtonsoft.Json.JsonTextReader.Read () [0x0004c] in <2073514815234917a5e8f91b0b239405>:0
at Newtonsoft.Json.JsonReader.ReadAndMoveToContent () [0x00000] in <2073514815234917a5e8f91b0b239405>:0
at Newtonsoft.Json.JsonReader.ReadForType (Newtonsoft.Json.Serialization.JsonContract contract, System.Boolean hasConverter) [0x0004a] in <2073514815234917a5e8f91b0b239405>:0
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType, System.Boolean checkAdditionalContent) [0x000db] in <2073514815234917a5e8f91b0b239405>:0
at Newtonsoft.Json.JsonSerializer.DeserializeInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x
JSON(与我的应用从服务器接收到的完全相同)
[{"type":"0","ID":"1","groupID":"0","authorID":"14","contents":"rfgwgfgdfssd","time_stamp":"0-0-0"}, {"type":"0","ID":"2","groupID":"0","authorID":"14","contents":"whwrgthwr","time_stamp":"0-0-0"}]
脚本
async void getUpdatesAsync()
{
Console.WriteLine("starting...");
BackendConnect connectionHandler = new BackendConnect("vocal/posts/index.php");
var updatesRequestInfo = new Dictionary<string, string>
{
{"sessionID", "c1f7a973c04e18a05342ecc2d16f7339"},
{"type", "updateFeed"},
{"userID", "14"}
};
connectionHandler.addData(updatesRequestInfo);
string updatesReturn = await connectionHandler.sendForm(); // returns the json
Console.WriteLine(updatesReturn); // the above json was copied and pasted from this output
List<Dictionary<string, string>> updatesArray = JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(updatesReturn); // this line causes error
Console.WriteLine(updatesArray);
//updatesArray.ForEach(Console.WriteLine);
}
我的应用程序中的另一个脚本也使用 JsonConvert.DeserializeObject<DataType>(stringToDeserialize) 在将单维数组 (["SUCCESS", "adlhfbaf"]) 提供给反序列化函数并且能够将其解析为简单列表时工作,但在尝试使用多维数组时失败上面的 JSON。
到目前为止,我已尝试将 updatesArray 变量设置为无数种不同的数据类型,以查看这是否是我怀疑的问题,但作为 C# 等语言的新手,您必须预先定义数据类型,我很难解决这个问题。
本质上,我想将它作为 C# 中的字典列表导入,以便我可以使用 foreach() 循环遍历初始列表的每个索引并恢复可以获取特定值(例如内容)的字典.下面是我要导入的示例(我知道语法不正确,但至少是一个粗略的想法)
var updatesArray = new List<Dictionary<string, string>>
{
{
{"type", "0"},
{"ID", "1"},
{"groupID", "0"},
{"authorID", "14"},
{"contents", "rfgwgfgdfssd"},
{"time_stamp", "0-0-0"}
},
{
{"type", "0"},
{"ID", "2"},
{"groupID", "0"},
{"authorID", "14"},
{"contents", "whwrgthwr"},
{"time_stamp", "0-0-0"}
}
};
【问题讨论】:
-
你能把你的json解析成一个JObject吗?
-
嗨,我不熟悉 JObjects,它是如何工作的?
-
而不是反序列化成字典列表。为您的响应创建一个模型并将其反序列化为该模型的列表。这也将使您对代码中的数据进行智能感知
-
我已经为您的字符串测试了
JsonConvert.Deserialize<List<Dictionary<string, string>>并且它有效。您的字符串实际上不同,或者您的List或Dictionary不是来自System.Collections.Generic命名空间。另外,您使用什么 Newtonsoft.Json 版本? -
JObject.Parse(jsonstring);
标签: c# list dictionary xamarin json.net