使用反序列化变量DataContractJsonSerializer类型JSON数组 [英] Deserializing variable Type JSON array using DataContractJsonSerializer

查看:181
本文介绍了使用反序列化变量DataContractJsonSerializer类型JSON数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个形式的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 object X 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数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆