[英]Type is not supported for deserialization of an array
I'm trying to dezerialize an array but I keep running into an error. 我正在尝试对数组进行解法,但我一直遇到错误。
JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
Profiles thingy = jsonSerializer.Deserialize<Profiles>(fileContents);
This is the code that gives me the error: 这是给我错误的代码:
Type is not supported for deserialization of an array.
This is how my JSON looks: 这就是我的JSON的样子:
[
{
"Number": 123,
"Name": "ABC",
"ID": 123,
"Address": "ABC"
}
]
You just need to deserialize it to a collection of some sort - eg an array. 您只需要将其反序列化为某种类型的集合 - 例如数组。 After all, your JSON does represent an array, not a single item. 毕竟,您的JSON 确实代表一个数组,而不是单个项目。 Short but complete example: 简短而完整的例子:
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
}
}
The JSON is a list. JSON是一个列表。 The square brackets in JSON indicate an array or list of objects. JSON中的方括号表示对象的数组或列表。 So you need to tell it to return a list of objects: 所以你需要告诉它返回一个对象列表:
JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
var profiles = jsonSerializer.Deserialize<List<Profiles>>(fileContents);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.