数组反序列化不支持Type [英] Type is not supported for deserialization of an array
本文介绍了数组反序列化不支持Type的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试对数组进行反序列化,但是我一直遇到错误.
I'm trying to dezerialize an array but I keep running into an error.
JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
Profiles thingy = jsonSerializer.Deserialize<Profiles>(fileContents);
这是给我错误的代码:
Type is not supported for deserialization of an array.
这是我的JSON的样子:
This is how my JSON looks:
[
{
"Number": 123,
"Name": "ABC",
"ID": 123,
"Address": "ABC"
}
]
推荐答案
您只需要将其反序列化为某种类型的集合-例如数组.毕竟,您的JSON 确实代表一个数组,而不是单个项目.简短但完整的示例:
You just need to deserialize it to a collection of some sort - e.g. an array. After all, your JSON does represent an array, not a single item. 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
}
}
这篇关于数组反序列化不支持Type的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文