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);
}
2
  • Check the documentation for JSON.Net. You can use Read to read through the JsonTextReader token by token for example. Commented Jan 22, 2015 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 Commented Jan 23, 2015 at 18:36

1 Answer 1

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


                }
            }

        }

}

Sign up to request clarification or add additional context in comments.

1 Comment

You could mark your own answer as accepted.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.