类型为“Newtonsoft.Json.JsonReaderException”的未处理异常

时间:2017-08-29 20:22:25

标签: c# json json.net

我的代码如下:

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文件?感谢

2 个答案:

答案 0 :(得分:1)

我已经通过reader.SupportMultipleContent = true;

解决了这个问题

答案 1 :(得分: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}]}]