2017-08-29 126 views
-1

我有下面這樣的代碼:類型的未處理的異常「Newtonsoft.Json.JsonReaderException」

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using Amazon.DynamoDBv2; 
using Amazon.DynamoDBv2.Model; 
using Amazon.Runtime; 
using System.IO; 
using Newtonsoft.Json; 
using Newtonsoft.Json.Linq; 
namespace ConsoleApplication2 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 


      using (FileStream fs = new FileStream(@"c:\Users\\Desktop\abc.json", FileMode.Open, FileAccess.Read)) 
      using (StreamReader sr = new StreamReader(fs)) 
      using (JsonTextReader reader = new JsonTextReader(sr)) 
      { 

       while (reader.Read()) 
       { 
        if (reader.TokenType == JsonToken.StartObject) 
        { 
         JObject obj = JObject.Load(reader); 
         var device_id = obj["device_id"]; 
         Console.WriteLine(device_id); 
        } 
       } 
       Console.ReadLine(); 

      } 
     } 


    } 
} 

文件abc.json含量也低於

{"_id":{"$oid":"552fcdd61718285a7402aeea"},"gateway_id":2,"device_id":28,"date_time":1.429196246258e+12,"data":[{"r":203,"v":2509},{"r":221,"v":3037},{"r":400,"v":0},{"r":401,"v":0},{"r":402,"v":0},{"r":403,"v":0},{"r":404,"v":0},{"r":405,"v":0},{"r":406,"v":0},{"r":407,"v":0},{"r":408,"v":0},{"r":409,"v":0},{"r":1000,"v":0},{"r":1001,"v":0},{"r":1002,"v":3},{"r":1003,"v":21895},{"r":1004,"v":2500}]} 
{"_id":{"$oid":"552fcdd61718285a7402aeeb"},"gateway_id":2,"device_id":29,"date_time":1.429196246989e+12,"data":[{"r":203,"v":2468},{"r":221,"v":2989},{"r":400,"v":0},{"r":401,"v":0},{"r":402,"v":0},{"r":403,"v":0},{"r":404,"v":0},{"r":405,"v":0},{"r":406,"v":0},{"r":407,"v":0},{"r":408,"v":0},{"r":409,"v":0},{"r":1000,"v":0},{"r":1001,"v":0},{"r":1002,"v":3},{"r":1003,"v":8841},{"r":1004,"v":2400}]} 
{"_id":{"$oid":"552fcdd71718285a7402aeec"},"gateway_id":2,"device_id":30,"date_time":1.429196247718e+12,"data":[{"r":203,"v":61536},{"r":221,"v":3055},{"r":400,"v":129},{"r":401,"v":32},{"r":402,"v":18064},{"r":403,"v":16435},{"r":404,"v":0},{"r":405,"v":0},{"r":406,"v":0},{"r":407,"v":0},{"r":408,"v":0},{"r":409,"v":0},{"r":1000,"v":1},{"r":1001,"v":1},{"r":1002,"v":3},{"r":1003,"v":20898},{"r":1004,"v":2100}]} 
{"_id":{"$oid":"552fcde31718285a7402aeed"},"gateway_id":1,"device_id":28,"date_time":1.429196259149e+12,"data":[{"r":203,"v":2419},{"r":221,"v":2969},{"r":400,"v":0},{"r":401,"v":0},{"r":402,"v":0},{"r":403,"v":0},{"r":404,"v":0},{"r":405,"v":0},{"r":406,"v":0},{"r":407,"v":0},{"r":408,"v":0},{"r":409,"v":0},{"r":1000,"v":1},{"r":1001,"v":1},{"r":1002,"v":3},{"r":1003,"v":26674},{"r":1004,"v":1400}]} 
{"_id":{"$oid":"552fcde31718285a7402aeee"},"gateway_id":1,"device_id":29,"date_time":1.429196259879e+12,"data":[{"r":203,"v":2440},{"r":221,"v":2882},{"r":400,"v":0},{"r":401,"v":0},{"r":402,"v":0},{"r":403,"v":0},{"r":404,"v":0},{"r":405,"v":0},{"r":406,"v":0},{"r":407,"v":0},{"r":408,"v":0},{"r":409,"v":0},{"r":1000,"v":1},{"r":1001,"v":1},{"r":1002,"v":3},{"r":1003,"v":22319},{"r":1004,"v":2200}]} 

該錯誤是在未來的行(reader.Read())。此外,上述代碼是否可以讀取最大1 GB的大型json文件?謝謝

+0

你可以發佈錯誤消息的詳細信息嗎? –

+0

JsonReaderException通常拋出一個相關的錯誤消息,通常會告訴你到底發生了什麼錯誤。請與我們分享所有例外情況。 – Maritim

+0

@Maritim附加信息:完成閱讀JSON內容後遇到的其他文本:{。路徑'',第2行,位置0.我認爲這是更詳細的 –

回答

1

我有固定的reader.SupportMultipleContent =真正的問題;

0

你存儲json錯誤。嘗試使用它

[{"_id":{"$oid":"552fcdd61718285a7402aeea"},"gateway_id":2,"device_id":28,"date_time":1.429196246258e+12,"data":[{"r":203,"v":2509},{"r":221,"v":3037},{"r":400,"v":0},{"r":401,"v":0},{"r":402,"v":0},{"r":403,"v":0},{"r":404,"v":0},{"r":405,"v":0},{"r":406,"v":0},{"r":407,"v":0},{"r":408,"v":0},{"r":409,"v":0},{"r":1000,"v":0},{"r":1001,"v":0},{"r":1002,"v":3},{"r":1003,"v":21895},{"r":1004,"v":2500}]}, 
{"_id":{"$oid":"552fcdd61718285a7402aeeb"},"gateway_id":2,"device_id":29,"date_time":1.429196246989e+12,"data":[{"r":203,"v":2468},{"r":221,"v":2989},{"r":400,"v":0},{"r":401,"v":0},{"r":402,"v":0},{"r":403,"v":0},{"r":404,"v":0},{"r":405,"v":0},{"r":406,"v":0},{"r":407,"v":0},{"r":408,"v":0},{"r":409,"v":0},{"r":1000,"v":0},{"r":1001,"v":0},{"r":1002,"v":3},{"r":1003,"v":8841},{"r":1004,"v":2400}]}, 
{"_id":{"$oid":"552fcdd71718285a7402aeec"},"gateway_id":2,"device_id":30,"date_time":1.429196247718e+12,"data":[{"r":203,"v":61536},{"r":221,"v":3055},{"r":400,"v":129},{"r":401,"v":32},{"r":402,"v":18064},{"r":403,"v":16435},{"r":404,"v":0},{"r":405,"v":0},{"r":406,"v":0},{"r":407,"v":0},{"r":408,"v":0},{"r":409,"v":0},{"r":1000,"v":1},{"r":1001,"v":1},{"r":1002,"v":3},{"r":1003,"v":20898},{"r":1004,"v":2100}]}, 
{"_id":{"$oid":"552fcde31718285a7402aeed"},"gateway_id":1,"device_id":28,"date_time":1.429196259149e+12,"data":[{"r":203,"v":2419},{"r":221,"v":2969},{"r":400,"v":0},{"r":401,"v":0},{"r":402,"v":0},{"r":403,"v":0},{"r":404,"v":0},{"r":405,"v":0},{"r":406,"v":0},{"r":407,"v":0},{"r":408,"v":0},{"r":409,"v":0},{"r":1000,"v":1},{"r":1001,"v":1},{"r":1002,"v":3},{"r":1003,"v":26674},{"r":1004,"v":1400}]}, 
{"_id":{"$oid":"552fcde31718285a7402aeee"},"gateway_id":1,"device_id":29,"date_time":1.429196259879e+12,"data":[{"r":203,"v":2440},{"r":221,"v":2882},{"r":400,"v":0},{"r":401,"v":0},{"r":402,"v":0},{"r":403,"v":0},{"r":404,"v":0},{"r":405,"v":0},{"r":406,"v":0},{"r":407,"v":0},{"r":408,"v":0},{"r":409,"v":0},{"r":1000,"v":1},{"r":1001,"v":1},{"r":1002,"v":3},{"r":1003,"v":22319},{"r":1004,"v":2200}]}] 
+0

至於大文件:我不建議這樣做,因爲文件的大部分不是數據,而是字段的名稱。使用db –

相關問題