1

我有下一个 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
}
]

这是我的 C# 类:

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; }
    }

我的反序列化:

var contents //JSON string above
Applications ApplicationsList = JsonConvert.DeserializeObject<Applications>(contents) as Applications;

代码失败:

无法将当前 JSON 数组(例如 [1,2,3])反序列化为类型“App1.MainPage+Applications”,因为该类型需要 JSON 对象(例如 {"name":"value"})才能正确反序列化。

要修复此错误,请将 JSON 更改为 JSON 对象(例如 {"name":"value"})或将反序列化类型更改为数组或实现集合接口的类型(例如 ICollection、IList),例如可以从 JSON 数组反序列化。JsonArrayAttribute 也可以添加到类型中以强制它从 JSON 数组反序列化。

路径'',第 1 行,位置 1。

用 json2csharp 检查了我的课程 - 一切都很好......问题出在哪里?

4

1 回答 1

3

您的 json 是一个数组,而不是单个对象。尝试这个:

var ApplicationsList = JsonConvert.DeserializeObject<List<Applications>>(contents);
于 2013-09-16T10:39:51.530 回答