What is the correct way to use JSON.NET to parse stream of JSON objects?
I think if you change your loop around slightly everything should work:
public IEnumerable<TResult> ReadJson<TResult>(Stream stream)
{
var serializer = new JsonSerializer();
using (var reader = new StreamReader(stream))
using (var jsonReader = new JsonTextReader(reader))
{
jsonReader.SupportMultipleContent = true;
while (jsonReader.Read())
{
yield return serializer.Deserialize<TResult>(jsonReader);
}
}
}
Note that you must iterate over the IEnumerable<TResult>
while the Stream
passed to this method is open:
using (var stream = /* some stream */)
{
IEnumerable<MyClass> result = ReadJson<MyClass>(stream);
foreach (var item in result)
{
Console.WriteLine(item.MyProperty);
}
}
Example: https://dotnetfiddle.net/Y2FLuK
Sample on JsonNet site: Read Multiple Fragments With JsonReader
How to parse huge JSON file as stream in Json.NET?
This should resolve your problem. Basically it works just like your initial code except it's only deserializing object when the reader hits the {
character in the stream and otherwise it's just skipping to the next one until it finds another start object token.
JsonSerializer serializer = new JsonSerializer();
MyObject o;
using (FileStream s = File.Open("bigfile.json", FileMode.Open))
using (StreamReader sr = new StreamReader(s))
using (JsonReader reader = new JsonTextReader(sr))
{
while (reader.Read())
{
// deserialize only when there's "{" character in the stream
if (reader.TokenType == JsonToken.StartObject)
{
o = serializer.Deserialize<MyObject>(reader);
}
}
}
Convert a Stream into json value c#
Above answer has a class defined. I didnt want to define different class as my model is dynamic. I found this solution , which worked well and got me the desired result
var serializer = new JsonSerializer();
using (var sr = new StreamReader(responseMessage))
using (var jsonTextReader = new JsonTextReader(sr))
{
var jsObj= serializer.Deserialize(jsonTextReader);
}
Can Json.NET serialize / deserialize to / from a stream?
UPDATE: This no longer works in the current version, see below for correct answer (no need to vote down, this is correct on older versions).
Use the JsonTextReader
class with a StreamReader
or use the JsonSerializer
overload that takes a StreamReader
directly:
var serializer = new JsonSerializer();
serializer.Deserialize(streamReader);
Read as much JSON data from the stream as possible
I assume you're going to use Json.Net library. In this case it will cater for your particular use case out of the box.
consider the following:
var s = "{ \"SomeData\": \"blahblah\", \"SubObject\": {\"SomeData\": \"blahblah}{{\" } } sdfsdfsdf... and some more data";
var obj1 = JsonConvert.DeserializeObject(s, new JsonSerializerSettings() {
CheckAdditionalContent = false // this is the key here, otherwise you will get an exception
});
JsonSerializer serializer = new JsonSerializer();
var obj2 = serializer.Deserialize(new JsonTextReader(new StringReader(s))); // no issues here either
How to parse a json array json.net
Not wanting to use classes is weird but not impossible.
var json = reader.ReadToEnd();
var objects = JsonConvert.DeserializeObject<dynamic[]>(json);
var lvi = new ListViewItem(new string[] { (string)objects[i].label, (string)objects[i].value });
Related Topics
How to Delete a Line from a Text File in C#
Why Are C# 3.0 Object Initializer Constructor Parentheses Optional
Web App Blocked While Processing Another Web App on Sharing Same Session
How to Make a .Net Windows Forms Application That Only Runs in the System Tray
Using Regex to Balance Match Parenthesis
Load an Exe File and Run It from Memory
How to Deal with Files with a Name Longer Than 259 Characters
How to Embed a Text File in a .Net Assembly
Create Folder and File on Current User Profile, from Admin Profile
How to Change the Table Names When Using ASP.NET Identity
How to Convert Seconds into (Hour:Minutes:Seconds:Milliseconds) Time