如何修复“无法将当前 JSON 数组反序列化为类型 'passages' 因为需要 JSON 对象(例如 {"name":"value"})来反序列化
How to fix 'Cannot deserialize the current JSON array into type 'passages' because requires a JSON object (e.g. {"name":"value"}) to deserialize
我正在尝试使用 c# 中的 newtonsoft.json 从本地目录读取和访问大型 JSON 文件,但总是给我错误。我创建了两个 类 来访问它。
这是我的示例 JSON 数据:
{
"passages": [
{
"passage_text": "xxxxxxx",
"url": "xxxxx",
}
]
"answer":"xxxxxx",
"query_id":"Xxxxx"
}
这是我试过的代码:
public class collection
{
public passages passages { get; set; }
public String answers { get; set; }
public String query_id { get; set; }
}
public class passages
{
public String url { get; set; }
public String passage_text { get; set; }
}
这是我试图阅读和访问 JSON 文件的部分:
String jsonPath = @"C:\Users\admin\Desktop7\project\collection\sample_collection.json" ;
var serializer = new JsonSerializer();
StreamReader sr = new StreamReader(jsonPath);
JsonTextReader reader = new JsonTextReader(sr);
reader.SupportMultipleContent = true;
while (reader.Read())
{
if (reader.TokenType == JsonToken.StartObject)
{
collection c = serializer.Deserialize<collection>(reader);
Console.WriteLine(c.passages.url);
}
}
它给了我这个错误:
Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'LuceneIndexApplication.passages' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path '[0].passages', line 1, position 15.'
您正在尝试将数组中的段落映射到列表中。
所以您需要更改的是:
public class collection
{
public passages passages { get; set; }
public String answers { get; set; }
public String query_id { get; set; }
}
应该是
public class collection
{
public List<passages> passages { get; set; }
public String answers { get; set; }
public String query_id { get; set; }
}
注意后面的List
我正在尝试使用 c# 中的 newtonsoft.json 从本地目录读取和访问大型 JSON 文件,但总是给我错误。我创建了两个 类 来访问它。 这是我的示例 JSON 数据:
{
"passages": [
{
"passage_text": "xxxxxxx",
"url": "xxxxx",
}
]
"answer":"xxxxxx",
"query_id":"Xxxxx"
}
这是我试过的代码:
public class collection
{
public passages passages { get; set; }
public String answers { get; set; }
public String query_id { get; set; }
}
public class passages
{
public String url { get; set; }
public String passage_text { get; set; }
}
这是我试图阅读和访问 JSON 文件的部分:
String jsonPath = @"C:\Users\admin\Desktop7\project\collection\sample_collection.json" ;
var serializer = new JsonSerializer();
StreamReader sr = new StreamReader(jsonPath);
JsonTextReader reader = new JsonTextReader(sr);
reader.SupportMultipleContent = true;
while (reader.Read())
{
if (reader.TokenType == JsonToken.StartObject)
{
collection c = serializer.Deserialize<collection>(reader);
Console.WriteLine(c.passages.url);
}
}
它给了我这个错误:
Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'LuceneIndexApplication.passages' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path '[0].passages', line 1, position 15.'
您正在尝试将数组中的段落映射到列表中。
所以您需要更改的是:
public class collection
{
public passages passages { get; set; }
public String answers { get; set; }
public String query_id { get; set; }
}
应该是
public class collection
{
public List<passages> passages { get; set; }
public String answers { get; set; }
public String query_id { get; set; }
}
注意后面的List