0

我是 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)

}

但它不起作用!不断抛出“参数异常”

请问有什么帮助吗?

4

2 回答 2

3

看起来您的 JSON 可能不正确。

AList更接近地映射到 JSON 数组 - 例如:

 [
       {"Name" : "Zeus","Age" : "1825","Height" : "900"},
       {"Name" : "Hera","Age" : "1805","Height" : "200"}
 ]

如果你想使用外花括号{},那么你可以序列化到/从一个Dictionary<string, TestClass>使用 JSON 像:

 {
       "Zeus" : {"Name" : "Zeus","Age" : "1825","Height" : "900"},
       "Hera" : {"Name" : "Hera","Age" : "1805","Height" : "200"}
 ]
于 2012-07-16T12:20:01.267 回答
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"}
     ]";
于 2012-07-16T12:22:02.083 回答