我正在尝试对数组进行解法,但我一直遇到错误.
JavaScriptSerializer jsonSerializer = new JavaScriptSerializer(); Profiles thingy = jsonSerializer.Deserialize<Profiles>(fileContents);
阵列的反序列化不支持Type.
这就是我的JSON的样子:
[ { "Number": 123,"Name": "ABC","ID": 123,"Address": "ABC" } ]
解决方法
您只需要将其反序列化为某种类型的集合 – 例如数组.毕竟,您的JSON确实代表一个数组,而不是单个项目.简短而完整的例子:
using System; using System.IO; using System.Web.Script.Serialization; public class Person { public string Name { get; set; } public string ID { get; set; } public string Address { get; set; } public int Number { get; set; } } class Test { static void Main() { var serializer = new JavaScriptSerializer(); var json = File.ReadAllText("test.json"); var people = serializer.Deserialize<Person[]>(json); Console.WriteLine(people[0].Name); // ABC } }