簡體   English   中英

使用JSON.NET反序列化嵌套的JSON響應

[英]Deserialize nested JSON Response with JSON.NET

我已經在SO上發布了一個類似的問題 ,在哪里詢問如何使用REST Api並反序列化嵌套的JSON Response。 我想將其用於.NET MVC5 Web應用程序,但事實證明它太慢了(響應大約需要5分鍾)。 我現在想做的是將json響應保存到文件中,然后從Web應用程序訪問本地文件(我還打算稍后實現緩存)。

JSON響應如下所示:

{
  "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);
}

但這引發了以下錯誤:

Newtonsoft.Json.dll中發生類型為'Newtonsoft.Json.JsonSerializationException'的異常,但未在用戶代碼中處理

附加信息:無法反序列化當前JSON對象(例如{“ name”:“ value”})為類型'System.Collections.Generic.List`1 [CourseProviders.ExternalProviders.PocoCourseTypeDescription]',因為該類型需要JSON數組(例如[1,2,3])正確反序列化。

要解決此錯誤,可以將JSON更改為JSON數組(例如[1,2,3]),也可以更改反序列化類型,使其成為普通的.NET類型(例如,不像整數這樣的原始類型,也不像這樣的集合類型)數組或列表),可以從JSON對象反序列化。 還可以將JsonObjectAttribute添加到類型中,以強制其從JSON對象反序列化。

我怎樣才能解決這個問題?

根據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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM