-1

i have this code below:

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();

            }
        }


    }
}

The file abc.json content is also below

{"_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}]}

The error is coming on the line while (reader.Read()). Moreover, Is the above code fine to read the large json file up to 1 GB? thanks

Ali Nouman
  • 3,304
  • 9
  • 32
  • 53
  • can you post the details of the error message? – Afnan Makhdoom Aug 29 '17 at 20:26
  • The JsonReaderException is typically thrown with an associated error message which normally tells you exactly what's wrong. Please share all the exception details with us. – Maritim Aug 29 '17 at 20:27
  • @Maritim Additional information: Additional text encountered after finished reading JSON content: {. Path '', line 2, position 0. i think that is more detail – Ali Nouman Aug 29 '17 at 20:29
  • 1
    @HumanLove - update your question with that detail, don't place it here as a comment. When you have an Exception always include the Type, Message, and the stack trace. Repeat this recursively for any inner exception. – Igor Aug 29 '17 at 20:30
  • Please add the details to your question. I believe the problem here is that you're not reading the file line by line, and your json objects aren't comma separated. – Maritim Aug 29 '17 at 20:30

2 Answers2

1

i have fix the issue by reader.SupportMultipleContent = true;

Ali Nouman
  • 3,304
  • 9
  • 32
  • 53
0

You stored json wrong. Try to use it

[{"_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}]}]
  • As for the large file: I do not advise doing this, because the big part of the file is not the data, but the names of the fields. Use db – A. Lukianovich Aug 29 '17 at 20:45