Reputation: 15931
I am trying to deserialize the following JSON representation to a strongly typed object. I am able to serialize it from c# -> json, but not vice versa.
C#
public class Package
{
public Guid Id {get;set;}
public List<string> Email {get;set;}
public List<Items> Items {get;set;}
}
public class Items
{
public string Uri {get;set;}
public int Width {get;set;}
public int Height {get;set;}
}
JSON
{
"Id":"84fd8751-6107-41af-9473-65aae51e042a",
"Email":[
"[email protected]"
],
"Items":"[
{"Uri":"http://localhost/foo.jpg","Width":234,"Height":313},
{"Uri":"http://localhost/bar.jpg","Width":234,"Height":174}]"
}
Code to deserialize
var instance = JsonSerializer.DeserializeFromString<Package>(jsonData);
The object instance
is created, and there are 2 item objects in instance.Items
but all of their properties are null.
TIA
Upvotes: 2
Views: 1058
Reputation: 71384
That is not valid JSON. The quotation marks around the value for Items should not be there.
Upvotes: 3
Reputation: 7898
You have quotes around the Items value, thus parsing them as a string instead of an array/list. Remove them to win.
"Items":[
{"Uri":"http://localhost/foo.jpg","Width":234,"Height":313},
{"Uri":"http://localhost/bar.jpg","Width":234,"Height":174}]
Upvotes: 6