How to deserialize Json String dynamically

前端 未结 2 1529
醉话见心
醉话见心 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: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. Something like this:

    using System.IO;
    using Newtonsoft.Json;
    
    public class RootObject
    {
        public List 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 dataObjects = new List();
    
    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(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(JSON)

      The final product will look like this:

      [ {"data":["value1","value2","value3", (...)]},
        {"data":["value1","value2","value3", (...)]} ]
      

      List dataObjects = new List();
      
      using (StreamReader sr = new StreamReader(stream))
      {
          var JSONObject = sr.ReadToEnd();
          dataObjects = JsonConvert.DeserializeObject>(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
      

      提交回复
      热议问题