【发布时间】:2013-10-30 16:28:41
【问题描述】:
我有以下课程:
public class Test
{
public Dictionary<string, string> dict = new Dictionary<string, string>();
public static void main(String args[]){
var serializer = new JavaScriptSerializer();
Test tt = new Test();
tt.dict.Add("hello","divya");
tt.dict.Add("bye", "divya");
String s = serializer.Serialize(tt.dict); // s is {"hello":"divya","bye":"divya"}
Test t = (Test)serializer.Deserialize(s,typeof(Test));
Console.WriteLine(t.dict["hello"]); // gives error since dict is empty
}
所以问题是如何将像 {"hello":"divya","bye":"divya"} 这样的 json 字符串反序列化为包含字典的强类型对象。
【问题讨论】:
-
您正在序列化字典并反序列化为您的
Test类型。这不匹配。
标签: c# .net json serialization dictionary