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:
[
{
"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. 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. The square brackets in JSON indicate an array or list of objects. So you need to tell it to return a list of objects:
JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
var profiles = jsonSerializer.Deserialize<List<Profiles>>(fileContents);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.