我有一个这种形式的 JSON 字符串:
I have a JSON string in this form:
string jsonStr = "["A", ["Martini", "alovell"],["Martin", "lovell"]]"
我正在尝试使用带有以下代码片段的 C# .NET 反序列化器 DataContractJsonSerializer 对 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 object X should be
如果我的字符串是
jsonStr = "[["Martini", "alovell"],["Martin", "lovell"]]"
我可以用这个:
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 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!