问题描述
我正在尝试使用DataContractJsonSerializer反序列化JSON字符串,如下所示:
I'm trying to use DataContractJsonSerializer to deserialize a JSON string, that looks like:
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Dictionary< string,object []>)));
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Dictionary<string, object[]>));
ser. ReadObject();
ser.ReadObject();
此输出始终是一个空字典,没有例外.
The output for this is always an empty dictionary with no exceptions.
我的问题是类型参数应在构造函数中是什么?我注意到,还有另一个构造函数要求一个knownTypes,是否可能需要为对象数组中的内容放置一个int和bool列表?我对此有点疑惑.请帮忙!
My question is what should the type parameter be in the constructor? I've noticed that there's another constructor that asks for a knownTypes, could I have perhaps need to put a list of int and bool for what's in the object array? I'm kinda puzzled of what's wrong with this. Please help!
谢谢!
推荐答案
要使用DataContractJsonSerializer将任意JSON对象读入字典,可以使用ISerializable类型:
To read an arbitrary JSON object into a dictionary using the DataContractJsonSerializer, you can use an ISerializable type:
public 类 |
{ |
静态 string json = " {\" p1 \ ":[123,true],\\" p2 \ ":[456,́false]}" ; |
.[可序列化] |
public 类 MyCustomDict:ISerializable |
.{ |
public 字典< 字符串 , 对象 []>字典 |
public MyCustomDict() |
{ |
新 Dictionary< string , object []>(); |
受保护的 MyCustomDict(SerializationInfo info,StreamingContext context) |
{ |
新 Dictionary< string , object []>(); |
foreach (可变项 中 信息) |
Debug.Assert(entry.ObjectType.IsArray); |
对象 [] array = entry.Value 作为 object []; |
dict.Add(entry.Name,array); |
public void GetObjectData(SerializationInfo信息,StreamingContext上下文) |
{ |
foreach (( 字符串 键 in dict.键) |
``info.AddValue(key,dict [key]); |
静态 void Main( 字符串 [] args) |
{ |
DataContractJsonSerializerdcjs = new DataContractJsonSerializer( typeof (MyCustomDict)); |
MemoryStream ms = 新 MemoryStream(Encoding .UTF8.GetBytes(json)); |
``MyCustomDict''dict =(MyCustomDict)dcjs.ReadObject(ms); |
Console.WriteLine(dict.dict.Count); |
public class DeserializingArbitraryJsonObjects |
{ |
static string json = "{\"p1\": [123, true], \"p2\":[456, false]}"; |
[Serializable] |
public class MyCustomDict : ISerializable |
{ |
public Dictionary<string, object[]> dict; |
public MyCustomDict() |
{ |
dict = new Dictionary<string, object[]>(); |
} |
protected MyCustomDict(SerializationInfo info, StreamingContext context) |
{ |
dict = new Dictionary<string, object[]>(); |
foreach (var entry in info) |
{ |
Debug.Assert(entry.ObjectType.IsArray); |
object[] array = entry.Value as object[]; |
dict.Add(entry.Name, array); |
} |
} |
public void GetObjectData(SerializationInfo info, StreamingContext context) |
{ |
foreach (string key in dict.Keys) |
{ |
info.AddValue(key, dict[key]); |
} |
} |
} |
static void Main(string[] args) |
{ |
DataContractJsonSerializer dcjs = new DataContractJsonSerializer(typeof(MyCustomDict)); |
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)); |
MyCustomDict dict = (MyCustomDict)dcjs.ReadObject(ms); |
Console.WriteLine(dict.dict.Count); |
} |
} |
这篇关于DataContractJsonSerializer帮助反序列化Dictionary< string,object []>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!