我是Json序列化和反序列化的新手,
我有
class TestClass
{
public string Name{get;set;}
public string Age{get;set;}
public string Height{get;set;}
}
并具有以下序列化功能
public void SerializeData()
{
string jsonData = "{
{"Name" : "Zeus","Age" : "1825","Height" : "900"},
{"Name" : "Hera","Age" : "1805","Height" : "200"}
}";
var resultList = new List<TestClass>();
var ser = new JavaScriptSerializer();
resultList= serializer.Deserialize(jsonData , TestClass)
}
但它不起作用!不断抛出“论据例外”
请帮助吗?
答案 0 :(得分:3)
看起来您的JSON可能不正确。
List
更接近JSON数组 - 例如:
[
{"Name" : "Zeus","Age" : "1825","Height" : "900"},
{"Name" : "Hera","Age" : "1805","Height" : "200"}
]
如果你想使用外部花括号{}
,那么你可以使用JSON序列化到Dictionary<string, TestClass>
,如:
{
"Zeus" : {"Name" : "Zeus","Age" : "1825","Height" : "900"},
"Hera" : {"Name" : "Hera","Age" : "1805","Height" : "200"}
]
答案 1 :(得分:1)
这不代表数组:
string jsonData = "{
{"Name" : "Zeus","Age" : "1825","Height" : "900"},
{"Name" : "Hera","Age" : "1805","Height" : "200"}
}";
为了拥有一个数组,你应该:
string jsonData = "[
{"Name" : "Zeus","Age" : "1825","Height" : "900"},
{"Name" : "Hera","Age" : "1805","Height" : "200"}
]";