【发布时间】:2017-10-16 03:12:27
【问题描述】:
我正在尝试使用 Newtonsoft 反序列化一个数组,以便我可以在列表框中显示来自基于云的服务器的文件,但无论我尝试什么,我最终都会收到此错误:
Newtonsoft.Json.JsonReaderException: '解析值时遇到意外字符: [.路径 '[0].priv',第 4 行,位置 15。'
这是一个尝试反序列化的示例:
[
{
"code": 200,
"priv": [
{
"file": "file.txt",
"ext": "txt",
"size": "104.86"
},
{
"file": "file2.exe",
"ext": "exe",
"size": "173.74"
},
],
"pub": [
{
"file": "file.txt",
"ext": "txt",
"size": "104.86"
},
{
"file": "file2.exe",
"ext": "exe",
"size": "173.74"
}
]
}
]
我尝试使用这样的 C# 类:
public class ListJson
{
[JsonProperty("pub")]
public List List { get; set; }
}
public class List
{
[JsonProperty("file")]
public string File { get; set; }
[JsonProperty("ext")]
public string Ext { get; set; }
[JsonProperty("size")]
public string Size { get; set; }
}
[JsonProperty("priv")]
public List List { get; set; }
}
public class List
{
[JsonProperty("file")]
public string File { get; set; }
[JsonProperty("ext")]
public string Ext { get; set; }
[JsonProperty("size")]
public string Size { get; set; }
}
然后反序列化:
List<list> fetch = Newtonsoft.Json.JsonConvert.DeserializeObject<List<list>>(json);
【问题讨论】:
-
你用它来验证你的JSON 提示它有语法错误
-
你有 2 个类叫做
List...? -
类名 List 令人困惑
-
另外,还有一个名为
List的 C# 类 - 请不要混淆已经存在的类的名称。 -
如果您使用的是 Visual Studio - 您可以使用 (ctrl+c) 将 JSON 复制到内存中。单击编辑->选择性粘贴->将Json粘贴为类。
标签: c# json serialization json.net deserialization