I have consume Survey data via SSE Stream
that gives me each persons answers line by line with this format for Survey X
:
{\"data\":[\"4
why are you using StreamReader for reading JSON data?
you can use JObject to parse your json and get data out of it.
var json = @"{""data"":[""4482359"",""12526"",""5"",""5"","""",""Yes, that is right"",""1""]}";
var obj = JObject.Parse(json);
List<string> lst = (obj["data"]).ToObject<List<string>>();
var result = JsonConvert.SerializeObject(lst);
Assuming that this is the content of the stream:
{"data":["4482359","12526","5","5","","Yes, that is right","1"]}
{"data":["2223446","65432","3","3","","Nope, that's not right","0"]}
(...)
you can deserialize these lines using a List<class>
. Something like this:
using System.IO;
using Newtonsoft.Json;
public class RootObject
{
public List<string> data { get; set; }
}
That JSON streaming can be deserialize in two simple ways:
List<RootObject> dataObjects = new List<RootObject>();
using (StreamReader sr = new StreamReader(stream))
{
var JSONObject = sr.ReadToEnd();
var reader = new JsonTextReader(new StringReader(JSONObject)) { SupportMultipleContent = true };
var serializer = new JsonSerializer();
while (reader.Read()) {
dataObjects.Add(serializer.Deserialize<RootObject>(reader));
}
reader.Close();
}
VB.Net version:
Imports System.IO
Imports Newtonsoft.Json
Public Class RootObject
Public Property MyData As List(Of String)
End Class
Dim dataObjects As New List(Of RootObject)()
Using sr As New StreamReader(stream)
Dim JSONObject As String = sr.ReadToEnd()
Dim reader = New JsonTextReader(New StringReader(JSONObject)) With {
.SupportMultipleContent = True
}
Dim serializer = New JsonSerializer()
While reader.Read()
dataObjects.Add(serializer.Deserialize(Of RootObject)(reader))
End While
reader.Close()
End Using
[
before the first line and ]
after the last line, separating each line with a (comma). You have now a functional array that can be deserialized with JsonConvert.DeserializeObject<Object>(JSON)The final product will look like this:
[ {"data":["value1","value2","value3", (...)]},
{"data":["value1","value2","value3", (...)]} ]
List<RootObject> dataObjects = new List<RootObject>();
using (StreamReader sr = new StreamReader(stream))
{
var JSONObject = sr.ReadToEnd();
dataObjects = JsonConvert.DeserializeObject<List<RootObject>>(JSONObject);
}
VB.Net version:
Dim dataObjects As New List(Of RootObject)()
Using sr As New StreamReader(stream)
Dim JSONObject As String = sr.ReadToEnd()
dataObjects = JsonConvert.DeserializeObject(Of List(Of RootObject))(JSONObject)
End Using