使用JSON.NET反序列化嵌套的JSON响应

时间:2014-11-18 11:01:31

标签: c# json json.net deserialization

我已经在SO上发布了similar question,我问过,如何使用REST Api并反序列化嵌套的JSON响应。 我想将它用于.NET MVC5 Web应用程序,但事实证明它太慢了(响应需要大约5分钟)。我现在要做的是将json响应保存到文件并从Web应用程序访问本地文件(我还计划稍后实现缓存)。

这就是JSON Response的样子:

{
  "success": true,
  "message": "OK",
  "types": 
  [
    {
      "name": "A5EF3-ASR",
      "title": "ITIL Foundation Plus Cloud Introduction",
      "classroomDeliveryMethod": "Self-paced Virtual Class",
      "descriptions": {
        "EN": {
          "description": "some Text null",
          "overview": null,
          "abstract": "Some other text",
          "prerequisits": null,
          "objective": null,
          "topic": null
        }
      },
      "lastModified": "2014-10-08T08:37:43Z",
      "created": "2014-04-28T11:23:12Z"
    },
    {
      "name": "A4DT3-ASR",
      "title": "ITIL Foundation eLearning Course + Exam",
      "classroomDeliveryMethod": "Self-paced Virtual Class",
      "descriptions": {
        "EN": {
          "description": "some Text"
          (...)
    }
  ]
}

这就是我的POCO课程的样子(当我使用其余的api时哪个工作正常):

public class PocoCourse
{
    public bool Success { get; set; }
    public string Message { get; set; }
    public List<PocoCourseType> Types { get; set; }
}

public class PocoCourseType
{
    public string Name { get; set; }
    public string Title { get; set; }
    public string ClassroomDeliveryMethod { get; set; }
    public List<PocoCourseTypeDescription> Descriptions { get; set; }
    public DateTime LastModified { get; set; }
    public DateTime Created { get; set; }
}
public class PocoCourseTypeDescription
{
    public List<PocoCourseTypeDescriptionDetails> EN { get; set; }
    public List<PocoCourseTypeDescriptionDetails> DE { get; set; }
}
public class PocoCourseTypeDescriptionDetails
{
    public string Description { get; set; }
    public string Overview { get; set; }
    public string Abstract { get; set; }
    public string Prerequisits { get; set; }
    public string Objective { get; set; }
    public string Topic { get; set; }
}

现在,我如何反序列化我的JSON文件的内容? 我尝试使用JSON.NET(Newtonsoft.Json)来执行此操作:

string filepath = HttpContext.Current.Server.MapPath("~/Files/Courses.json");

using (StreamReader r = new StreamReader(filepath))
{
    string json = r.ReadToEnd();
    PocoCourse items = JsonConvert.DeserializeObject<PocoCourse>(json);
}

但它引发了以下错误:

  

类型&#39; Newtonsoft.Json.JsonSerializationException&#39;的例外情况发生在Newtonsoft.Json.dll但未在用户代码中处理

     

附加信息:无法将当前JSON对象(例如{&#34; name&#34;:&#34; value&#34;})反序列化为类型System.Collections.Generic.List`1 [ CourseProviders.ExternalProviders.PocoCourseTypeDescription]&#39;因为该类型需要JSON数组(例如[1,2,3])才能正确反序列化。

     

要修复此错误,请将JSON更改为JSON数组(例如[1,2,3])或更改反序列化类型,使其成为普通的.NET类型(例如,不是像整数这样的基本类型,而不是可以从JSON对象反序列化的集合类型,如数组或List)。 JsonObjectAttribute也可以添加到类型中以强制它从JSON对象反序列化。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:4)

根据json示例,您的类定义应如下所示

public class PocoCourse
{
    public bool Success { get; set; }
    public string Message { get; set; }
    public List<PocoCourseType> Types { get; set; }
}

public class PocoCourseType
{
    public string Name { get; set; }
    public string Title { get; set; }
    public string ClassroomDeliveryMethod { get; set; }
    public PocoCourseTypeDescriptionContainer Descriptions { get; set; }
    public DateTime LastModified { get; set; }
    public DateTime Created { get; set; }
}

public class PocoCourseTypeDescription
{
    public string Description { get; set; }
    public string Overview { get; set; }
    public string Abstract { get; set; }
    public string Prerequisits { get; set; }
    public string Objective { get; set; }
    public string Topic { get; set; }
}
public class PocoCourseTypeDescriptionContainer
{
    public PocoCourseTypeDescription EN { get; set; }
    public PocoCourseTypeDescription DE { get; set; }
}

以下是一个有效的例子:https://dotnetfiddle.net/uvPV5l

相关问题