我正在尝试使用Powershell调用名称空间Newtonsoft.Json来处理大型Json文件。每个文件都包含数百万个JSON记录,但并非所有记录都是正确的格式。我正在使用Newtonsoft.Json.JsonTextReader,因为它每次都会读取一个json记录,而不是将整个文件读入内存。但是,当它读取包含某种格式问题的记录时,它将失败,并且我不知道如何忽略当前记录。 有什么方法可以让它忽略当前记录并继续下一个记录吗?
Exception calling "Deserialize" with "1" argument(s): "After parsing a value an unexpected character was encountered: ". Path 'records[46796].properties.userAgent', line 374378, position 634."
At line:6 char:13
+ $single=$analyzer.Deserialize($reader)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : JsonReaderException
Exception calling "Read" with "0" argument(s): "After parsing a value an unexpected character was encountered: ". Path 'records[46796].properties.userAgent', line 374378, position 634."
At line:1 char:7
+ while($reader.read()){
+ ~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : JsonReaderException
以下是遇到格式问题时的例外情况:
{
"Record":
[
{
"P1":"data",
"P2":"data",
"P3":"data"
}
,
{
"P1":"data2",
"P2":"data2",
"P3":"data2"
}
,
{
"P1":"data3",
"P2":"data3"",
"P3":"data3"
}
,
{
"P1":"data4",
"P2":"data4",
"P3":"data4"
}
]
}
这是一个示例json文件,如您所见,第三条记录的属性“ P2”存在格式问题。
function Foo(name, age){
this.name = name;
this.age = age;
this.announce = function(){
alert(this.name + " is " + this.age + " years old");
};
}
var myFoo = new Foo("John", 42);
答案 0 :(得分:0)
Try Catch语句怎么样?
try {
$single=$analyzer.Deserialize($reader)
# Deal with the json record...
$i++ }
catch {
LogWrite ("Caught the exception")
LogWrite ($Error[0].Exception)
$i++
}
}