我有一堆我需要处理的JSON文件; 我是这样做的:
var settings = new JsonSerializerSettins
{
Error = delegate(object sender, Newtonsoft.Json.Serialization.ErrorEventArgs args)
{
Log_The_Error();
args.ErrorContext.Handled = true;
}
};
foreach (var jsonFile in directory.GetFiles("*.json"))
{
using (StreamReader file = File.OpenText(jsonFile.FUllName))
{
try
{
var data = JsonConvert.DeserializeObject<JObject>(file.ReadToEnd(), settings);
if (data != null)
{
// Do fancy things...
}
}
catch (Exception e)
{
Log_The_Exception();
}
}
}
Run Code Online (Sandbox Code Playgroud)
一些json文件似乎被截断,因此内部json对象无效但是,即使数据被截断,我也可以使用已经收到的部分数据,json文本如下所示:
{
"property1": 1,
"property2": 2,
"property3": 3,
"property4": 4,
"array": {
"amount": 9999
"array": [
{ ... },
{ ... },
... Hundreds of json objects ...
{ ... },
{ ... }
]
}
}
Run Code Online (Sandbox Code Playgroud)
某些文件在array.array级别被截断:
{
"property1": 1,
"property2": 2,
"property3": 3,
"property4": 4,
"array": {
"amount": 9999
"array": [
{ ... },
{ ... },
... Hundreds of json objects ...
{ ... },
... end of file ...
Run Code Online (Sandbox Code Playgroud)
每当发生这种情况时,JsonConvert.DeserializeObject返回null,有没有办法获取数据,直到解析错误发生而不是null值?
| 归档时间: |
|
| 查看次数: |
57 次 |
| 最近记录: |