DataContractJsonSerializer帮助反序列化Dictionary< string,object []> [英] DataContractJsonSerializer help to deserlize Dictionary<string, object[]>

查看:70
本文介绍了DataContractJsonSerializer帮助反序列化Dictionary< string,object []>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用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!

谢谢!

推荐答案

字典在JSON中作为JSON数组进行序列化KeyValuePair< K,V> -由于CLR中可以使用字符串以外的其他类型作为键,因此它不能直接映射到JSON对象.

Dictionaries are serialized in JSONas arrays of KeyValuePair<K, V> - since one can use types other than strings as the key in CLR, it doesn't map directly to a JSON Object.

要使用DataContractJsonSerializer将任意JSON对象读入字典,可以使用ISerializable类型:

To read an arbitrary JSON object into a dictionary using the DataContractJsonSerializer, you can use an ISerializable type:

<身体>
public
{
静态 string json = " {\" ":[123,true],\\" p2 \ ":[456,́false]}" ;
.[可序列化]
public MyCustomDict:ISerializable
.{
public 字典< 字符串 对象 []>字典
public MyCustomDict()
{
Dictionary< []>();
受保护的 MyCustomDict(SerializationInfo info,StreamingContext context)
{
Dictionary< []>();
foreach (可变项 信息)
Debug.Assert(entry.ObjectType.IsArray);
对象 [] array = entry.Value [];
dict.Add(entry.Name,array);
public void GetObjectData(SerializationInfo信息,StreamingContext上下文)
{
foreach (( 字符串 in dict.键)
``info.AddValue(key,dict [key]);
静态 void Main( 字符串 [] args)
{
DataContractJsonSerializerdcjs = new DataContractJsonSerializer( (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<stringobject[]> dict;  
            public MyCustomDict()  
            {  
                dict = new Dictionary<stringobject[]>();  
            }  
            protected MyCustomDict(SerializationInfo info, StreamingContext context)  
            {  
                dict = new Dictionary<stringobject[]>();  
                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&lt; string,object []&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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