问题描述
我在这个形式的JSON字符串:
I have a JSON string in this form:
string jsonStr = "[\"A\", [\"Martini\", \"alovell\"],[\"Martin\", \"lovell\"]]"
我想用C#.NET解串器DataContractJsonSerializer具有以下code段反序列化的JSON
I am trying to deserialize the JSON using the C# .NET deserializer DataContractJsonSerializer with the following code snippet
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonStr));
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof<X>);
X data = (X)serializer.ReadObject(ms);
现在,因为JSON数组是可变类型的数组,我不知道是什么类型的对象
X应该是
Now since the JSON array is an array of variable types I do not know what type of objectX should be
如果我的字符串为
jsonStr = "[[\"Martini\", \"alovell\"],[\"Martin\", \"lovell\"]]"
我可以使用这样的:
I could use this:
X = List<List<String>>
和,将工作对我来说。我不知道是否有什么办法可以反序列化的变量类型JSON数组?
and that would work for me. I was wondering if there is any way to deserialize variable type JSON array?
推荐答案
您可以使用 Json.NET 做到这一点。
You could use Json.NET to do this.
JArray a = JArray.Parse(jsonStr);
该JArray将包含字符串或嵌套JArray的视JSON。
The JArray would contain either strings or nested JArray's depending on the JSON.
这篇关于使用反序列化变量DataContractJsonSerializer类型JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!