Reputation: 4331
I have next JSON:
[
{
"ApplicationRelations":[
{
"Application":null,
"ApplicationSubcategory":null,
"ApplicationCategory":{
"categoryName":"Default Category",
"id":4
},
"roleOrder":null,
"categoryOrder":null,
"subcategoryOrder":null,
"applicationOrder":null,
"id":6
},
{
"Application":{
"launchUrl":"link",
"trainingUrl":"link",
"installUrl":"link",
"vpn":true,
"overview":"text",
"summary":"text",
"id":12,
"title":"Application"
},
"ApplicationSubcategory":{
"subcategoryName":"Creation",
"id":9
},
"ApplicationCategory":{
"categoryName":"Default Category",
"id":4
},
"roleOrder":15,
"categoryOrder":25,
"subcategoryOrder":35,
"applicationOrder":45,
"id":15
}
],
"roleName":"Role 02",
"roleHeader":"Header of Role 02",
"AnnouncementRelations":[
],
"id":2
}
]
And here are my C# clases:
public class Applications
{
public List<SalesCentralApplicationRelation> salesCentralApplicationRelations { get; set; }
public string RoleName { get; set; }
public string RoleHeader { get; set; }
public List<object> SalesCentralAnnouncementRelations { get; set; }
public int Id { get; set; }
}
public class SalesCentralApplicationRelation
{
public SalesCentralApplication salesCentralApplication { get; set; }
public SalesCentralApplicationSubcategory salesCentralApplicationSubcategory { get; set; }
public SalesCentralApplicationCategory salesCentralApplicationCategory { get; set; }
public int roleOrder { get; set; }
public int categoryOrder { get; set; }
public int subcategoryOrder { get; set; }
public int applicationOrder { get; set; }
public int id { get; set; }
}
public class SalesCentralApplicationCategory
{
public string categoryName { get; set; }
public int id { get; set; }
}
public class SalesCentralApplicationSubcategory
{
public string subcategoryName { get; set; }
public int id { get; set; }
}
public class SalesCentralApplication
{
public string launchUrl { get; set; }
public string trainingUrl { get; set; }
public string installUrl { get; set; }
public bool vpn { get; set; }
public string overview { get; set; }
public string summary { get; set; }
public int id { get; set; }
public string title { get; set; }
}
And my deserialization:
var contents //JSON string above
Applications ApplicationsList = JsonConvert.DeserializeObject<Applications>(contents) as Applications;
And code fails with:
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'App1.MainPage+Applications' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path '', line 1, position 1.
Checked my classes with json2csharp - all is fine... where is the problem?
Upvotes: 1
Views: 133
Reputation: 35363
Your json is an array, not a single object. Try this:
var ApplicationsList = JsonConvert.DeserializeObject<List<Applications>>(contents);
Upvotes: 3