簡體   English   中英

使用Newtonsoft.Json反序列化JSON對象

[英]Deserealizing JSON object using Newtonsoft.Json

我有API端點,即返回我的JSON對象

就這個

 {
    "results": [
        {
            "id": 182,
            "title": "1-Day Private Beijing Tour to Tian'anmen Square, Forbidden City and Badaling Great Wall",
            "price": "162",
            "duration": "8",
            "duration_type": "1",
            "cover_image": {
                "id": 308,
                "img_path": "upload/images",
                "img_file": "6d637884086151b30fe12db52fbaf5eb.jpg",
                "status": "",
                "created_at": "2018-02-27 02:25:36",
                "updated_at": "2018-02-27 02:25:36",
                "destination_id": "182",
                "is_cover": "0",
                "url": "https://api.xplorpal.com/upload/images/300x300/6d637884086151b30fe12db52fbaf5eb.jpg"
            }
        },
        {
            "id": 183,
            "title": "One Day Private Beijing Tour to Mutianyu Great Wall and Summer Palace ",
            "price": "197",
            "duration": "8",
            "duration_type": "1",
            "cover_image": {
                "id": 305,
                "img_path": "upload/images",
                "img_file": "1f8a09ddffb80ef9232f3511893ae5c4.jpg",
                "status": "",
                "created_at": "2018-02-27 02:22:19",
                "updated_at": "2018-03-01 23:01:55",
                "destination_id": "183",
                "is_cover": "0",
                "url": "https://api.xplorpal.com/upload/images/300x300/1f8a09ddffb80ef9232f3511893ae5c4.jpg"
            }
        }
 ]
}

我需要反序列化

所以我寫了這個模型

    public class CoverImage
{
    public int id { get; set; }
    public string img_path { get; set; }
    public string img_file { get; set; }
    public string status { get; set; }
    public string created_at { get; set; }
    public string updated_at { get; set; }
    public string destination_id { get; set; }
    public string is_cover { get; set; }
    public string url { get; set; }
}

public class Result
{
    public int id { get; set; }
    public string title { get; set; }
    public string price { get; set; }
    public string duration { get; set; }
    public string duration_type { get; set; }
    public CoverImage cover_image { get; set; }
}

public class RootObject
{
    public List<Result> results { get; set; }
}

並嘗試這樣

var responseExperiences = JsonConvert.DeserializeObject<IEnumerable<RootObject>>(content);

但是,當我運行項目時,出現以下錯誤:

無法反序列化當前JSON對象(例如{“ name”:“ value”})為類型'System.Collections.Generic.IEnumerable`1 [TravelApp.Models.GettingExperiences + Results]',因為該類型需要JSON數組(例如[ 1,2,3])正確反序列化。 要解決此錯誤,可以將JSON更改為JSON數組(例如[1,2,3]),也可以更改反序列化類型,使其成為普通的.NET類型(例如,不像整數這樣的原始類型,也不像這樣的集合類型)數組或列表),可以從JSON對象反序列化。 還可以將JsonObjectAttribute添加到類型中,以強制其從JSON對象反序列化。

我該如何解決?

您的API返回的對象只有一個名為result屬性,而不是集合。 您應該反序列化為RootObject對象。

您的JSON顯示一個與RootObject對應的對象result

但是您正在嘗試反序列化RootObject數組IEnumerable<>

您應該使用它來反序列化顯示的JSON:

JsonConvert.DeserializeObject<RootObject>(content);

您的api返回一個名為result的單個對象, 不是一個需要簡單地像單個對象一樣反序列化的集合。

var responseExperiences = JsonConvert.DeserializeObject<RootObject>(content);

暫無
暫無

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

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