I have consume Survey data via SSE Stream
that gives me each persons answers line by line with this format for Survey X
:
{"data":["4482359","12526","5","5","","Yes, that is right","1"]}
I read it into a List(of String)
using Streamreader
Dim sr As StreamReader = New StreamReader(stream)
Dim myList As List(Of String) = New List(Of String)
While sr.Peek >= 0
myList.Add(sr.ReadLine())
End While
Now I want to deserialize the strings so I get the entries into properties of a class (let us call it tmpUserData
) that I can access. I have tried this using Newtonsoft.Json
:
Dim tmpUserData As Object = JsonConvert.DeserializeObject(Of Object)(myList(i))
but looking into tmpUserData
the deserialization does not split the entries as
(1) User1
(1) "4482359"
(2) "12526"
(3) "5"
(4) ""
...
(2) User1
(1) "5847895"
(2) "33568"
(3) "6"
(4) "2"
...
but instead it just put everything into one entry
(1) "4482359","12526","5","5","","Yes, that is right","1"
The reason why I cannot just write a class and put the data into it like
JsonConvert.DeserializeObject(myclass)(myList(i))
is that this has to be dynamically since the number and structure of strings differs for different surveys. So Survey Y
might looks like:
{"data":["Peter","Jackson","Albert Street","5","","1"]}
Could anyone help me get what I am looking for so the entries of each Survery Particpant are properly deserialized? C#
Solution is welcome as well.
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:
- If you don't really need to parse the stream line by line, read the stream to end, then use a JsonTextReader to read the resulting string and JsonSerializer to deserialize the reader's result into an instance of the class you're using as container for the data:
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
- If you need to parse the stream line by line for some reason not specified here, you could insert an array delimiter
[
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
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);