0

I would like to know if there is a way to read one json object at a time using Json.net. Currently, here is the code I'm using which works but loads the whole file in the streamreader but not able to just parse one object at a time. any suggestions??

StreamReader streamReader = new StreamReader(@"Sample.json");
List<Member> mlist; 

using (JsonReader reader = new JsonTextReader(streamReader))
{
    JsonSerializer serializer = new JsonSerializer();

    mlist = serializer.Deserialize<List<Member>>(reader);
}
Matt Burland
  • 44,552
  • 18
  • 99
  • 171
  • Check the documentation for JSON.Net. You can use `Read` to read through the `JsonTextReader` token by token for example. – Matt Burland Jan 22 '15 at 20:46
  • HI Matt - Using Read as i traverse token by token, how would i know a json object has ended and a new object has started? Thanks – Nimesh Mehta Jan 23 '15 at 18:36

1 Answers1

2

I was able to find the solution for my question with help of your comments and other links:

        StreamReader streamReader = new StreamReader(@"C:\Sample.json");
        using (JsonTextReader reader = new JsonTextReader(streamReader))
        {
            while (reader.Read())
            {
                if (reader.TokenType == JsonToken.StartObject)
                {
                    // Load each object from the stream and do something with it

                     JObject obj = JObject.Load(reader);

                     JsonSerializer serializer = new JsonSerializer();
                     Member m = (Member)serializer.Deserialize(new JTokenReader(obj), typeof(Member));


                }
            }

        }

}