我已通过SSE Stream
使用调查数据,该数据使我每个人都能按照Survey X
:的格式逐行回答
{"data":["4482359","12526","5","5","","Yes, that is right","1"]}
我使用Streamreader
将其读取为List(of String)
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
现在我想反序列化字符串,这样我就可以将条目输入到可以访问的类(让我们称之为tmpUserData
)的属性中。我已经用Newtonsoft.Json
:尝试过了
Dim tmpUserData As Object = JsonConvert.DeserializeObject(Of Object)(myList(i))
但在tmpUserData
中,反序列化并没有将条目拆分为
(1) User1
(1) "4482359"
(2) "12526"
(3) "5"
(4) ""
...
(2) User1
(1) "5847895"
(2) "33568"
(3) "6"
(4) "2"
...
但它只是把所有东西都放在一个条目中
(1) "4482359","12526","5","5","","Yes, that is right","1"
为什么我不能像一样只写一个类并将数据放入其中
JsonConvert.DeserializeObject(myclass)(myList(i))
这必须是动态的,因为不同调查的字符串数量和结构不同。所以Survey Y
可能看起来像:
{"data":["Peter","Jackson","Albert Street","5","","1"]}
有人能帮我得到我想要的东西吗?这样每个调查参与者的条目都能正确地反序列化?C#
解决方案也很受欢迎。
假设这是流的内容:
{"data":["4482359","12526","5","5","","Yes, that is right","1"]}
{"data":["2223446","65432","3","3","","Nope, that's not right","0"]}
(...)
您可以使用List<class>
反序列化这些行。类似这样的东西:
using System.IO;
using Newtonsoft.Json;
public class RootObject
{
public List<string> data { get; set; }
}
JSON流可以通过两种简单的方式进行反序列化:
- 如果您不需要逐行解析流,请将流读取到末尾,然后使用JsonTextReader读取结果字符串,并使用JsonSerializer将读取器的结果反序列化为用作数据容器的类的实例:
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版本:
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
- 如果由于此处未指定的某些原因需要逐行解析流,可以在第一行之前插入数组分隔符
[
,在最后一行之后插入数组分隔符号]
,用(逗号)分隔每一行。现在您有了一个可以使用JsonConvert.DeserializeObject<Object>(JSON)
反序列化的函数数组
最终产品将如下所示:
[ {"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版本:
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
为什么要使用StreamReader读取JSON数据?
您可以使用JObject解析json并从中获取数据。
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);