简体   繁体   English

将Json数组反序列化为C#列表<T>

[英]Deserializing Json Array to C# List<T>

I'm new at Json Serialization and Deserialization, 我是Json序列化和反序列化的新手,

I have 我有

class TestClass
{

    public string Name{get;set;}
    public string Age{get;set;}
    public string Height{get;set;}

}

and have the following serialization function 并具有以下序列化功能

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)

}

but it doesn't work! 但这不起作用! keeps throwing "Argument Exception" 不断抛出“参数异常”

Any Help Please? 有什么帮助吗?

It looks like your JSON might be incorrect. 看起来您的JSON可能不正确。

A List maps more closely to a JSON array - like: List更紧密地映射到JSON数组-如:

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

If you want to use outer curly braces {} then you can serialize to/from a Dictionary<string, TestClass> using JSON like: 如果要使用外部花括号{}则可以使用JSON序列化到Dictionary<string, TestClass>或从中进行序列化,如下所示:

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

this does not represent an array: 这不代表一个数组:

string jsonData = "{
           {"Name" : "Zeus","Age" : "1825","Height" : "900"},
           {"Name" : "Hera","Age" : "1805","Height" : "200"}
     }";

In order to have an array you should have: 为了拥有一个数组,您应该具有:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM