How to deserialize Json String dynamically

前端 未结 2 1524
醉话见心
醉话见心 2021-01-26 04:44

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         


        
相关标签:
2条回答
  • 2021-01-26 05:14

    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);
    
    0 讨论(0)
  • 2021-01-26 05:21

    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
    
    0 讨论(0)
提交回复
热议问题