如何反序列化具有 JSON 个对象的特殊数据集的文件
How to deserialize a file with peculiar data set of JSON objects
我正在尝试反序列化一个文件,该文件包含 JSON 个未用逗号分隔的对象的数据集
{
"passages": [
{
"is_selected": 1,
"url": "http://someUrl.com",
"passage_text": "someTextHere"
},
{
"is_selected": 0,
"url": "http://someUrl.com",
"passage_text": "someTextHere""
},
],
"query_id": 9749,
"answers": ["Here is the answer"],
"query_type": "description",
"query": "Here is my query"
}
{
"passages": [
{
"is_selected": 0,
"url": "http://someAnotherUrl.com",
"passage_text": "someAnotherTextHere"
},
{
"is_selected": 1,
"url": "http://someAnotherUrl.com",
"passage_text": "someAnotherTextHere""
},
],
"query_id": 0564,
"answers": ["Here is the another answer"],
"query_type": "another description",
"query": "Here is my another query"
}
我正在尝试将此代码与 Newtonsoft.JSON 反序列化一起使用,但它 returns 是一个例外
“'Newtonsoft.Json.JsonSerializationException' 类型的未处理异常发生在 Newtonsoft.Json.dll
附加信息:无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型 'System.Collections.Generic.List`1[JsonParser.DataSet]',因为该类型需要 JSON 数组(例如 [1,2,3])以正确反序列化。
public class DataSet
{
public List<Passage> passages { get; set; }
public int query_id { get; set; }
public List<string> answers { get; set; }
public string query_type { get; set; }
public string query { get; set; }
public int Count { get; internal set; }
}
public class Passage
{
public int is_selected { get; set; }
public Uri url { get; set; }
public string passage_text { get; set; }
}
class Program
{
public static string jsonFileLocation = @"C:\my.json";
static void Main(string[] args)
{
using (StreamReader file = File.OpenText(jsonFileLocation))
{
JsonSerializer serializer = new JsonSerializer();
List<DataSet> data = (List<DataSet>)serializer.Deserialize(file, typeof(List<DataSet>));
}
}
}
我认为手动放置逗号和括号以使文件成为 JSON 格式不是一个好主意,因为该文件包含数千条记录。
您对如何解析和反序列化此文件有任何想法吗?
如果文件一致,每个 JSON 节点由换行符分隔,您可以先将文件流式传输到 System.String 并替换所有实例:
}
{
与:
},
{
然后,反序列化 JSON 字符串。这只是一个想法。
我正在尝试反序列化一个文件,该文件包含 JSON 个未用逗号分隔的对象的数据集
{
"passages": [
{
"is_selected": 1,
"url": "http://someUrl.com",
"passage_text": "someTextHere"
},
{
"is_selected": 0,
"url": "http://someUrl.com",
"passage_text": "someTextHere""
},
],
"query_id": 9749,
"answers": ["Here is the answer"],
"query_type": "description",
"query": "Here is my query"
}
{
"passages": [
{
"is_selected": 0,
"url": "http://someAnotherUrl.com",
"passage_text": "someAnotherTextHere"
},
{
"is_selected": 1,
"url": "http://someAnotherUrl.com",
"passage_text": "someAnotherTextHere""
},
],
"query_id": 0564,
"answers": ["Here is the another answer"],
"query_type": "another description",
"query": "Here is my another query"
}
我正在尝试将此代码与 Newtonsoft.JSON 反序列化一起使用,但它 returns 是一个例外
“'Newtonsoft.Json.JsonSerializationException' 类型的未处理异常发生在 Newtonsoft.Json.dll
附加信息:无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型 'System.Collections.Generic.List`1[JsonParser.DataSet]',因为该类型需要 JSON 数组(例如 [1,2,3])以正确反序列化。
public class DataSet
{
public List<Passage> passages { get; set; }
public int query_id { get; set; }
public List<string> answers { get; set; }
public string query_type { get; set; }
public string query { get; set; }
public int Count { get; internal set; }
}
public class Passage
{
public int is_selected { get; set; }
public Uri url { get; set; }
public string passage_text { get; set; }
}
class Program
{
public static string jsonFileLocation = @"C:\my.json";
static void Main(string[] args)
{
using (StreamReader file = File.OpenText(jsonFileLocation))
{
JsonSerializer serializer = new JsonSerializer();
List<DataSet> data = (List<DataSet>)serializer.Deserialize(file, typeof(List<DataSet>));
}
}
}
我认为手动放置逗号和括号以使文件成为 JSON 格式不是一个好主意,因为该文件包含数千条记录。 您对如何解析和反序列化此文件有任何想法吗?
如果文件一致,每个 JSON 节点由换行符分隔,您可以先将文件流式传输到 System.String 并替换所有实例:
}
{
与:
},
{
然后,反序列化 JSON 字符串。这只是一个想法。