Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Newtonsoft.JSON反序列化JSON对象_C#_.net_Json_Json.net - Fatal编程技术网

C# 使用Newtonsoft.JSON反序列化JSON对象

C# 使用Newtonsoft.JSON反序列化JSON对象,c#,.net,json,json.net,C#,.net,Json,Json.net,我有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",

我有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; }
}
公共类封面图片
{
公共int id{get;set;}
公共字符串img_路径{get;set;}
公共字符串img_文件{get;set;}
公共字符串状态{get;set;}
在{get;set;}处创建的公共字符串
在{get;set;}处更新了公共字符串
公共字符串目标_id{get;set;}
公共字符串是_cover{get;set;}
公共字符串url{get;set;}
}
公开课成绩
{
公共int id{get;set;}
公共字符串标题{get;set;}
公共字符串price{get;set;}
公共字符串持续时间{get;set;}
公共字符串持续时间\u类型{get;set;}
公共封面图片封面图片{get;set;}
}
公共类根对象
{
公共列表结果{get;set;}
}
试着这样做

var responseExperiences = JsonConvert.DeserializeObject<IEnumerable<RootObject>>(content);
var-responseExperiences=JsonConvert.DeserializeObject(内容);
但当我运行该项目时,我有以下错误:

无法将当前JSON对象(例如{“名称”:“值”})反序列化为类型“System.Collections.Generic.IEnumerable`1[TravelApp.Models.GettingExperiences+Results]”,因为该类型需要JSON数组(例如[1,2,3])才能正确反序列化。 若要修复此错误,请将JSON更改为JSON数组(例如[1,2,3]),或更改反序列化类型,使其成为可以从JSON对象反序列化的正常.NET类型(例如,不是integer之类的基元类型,也不是数组或列表之类的集合类型)。还可以将JsonObjectAttribute添加到类型中,以强制它从JSON对象反序列化


如何修复此问题?

您的API返回一个名为
result
的对象,而不是一个集合。您应该反序列化为
RootObject
对象。

您的API返回一个名为
result
的对象,而不是一个集合。您应该反序列化为
RootObject
对象。

您的JSON显示一个与
RootObject
对应的
对象
结果

但是您正在尝试反序列化
RootObject的数组(
IEnumerable

您应该使用它来反序列化JSON,如下所示:

JsonConvert.DeserializeObject<RootObject>(content);
JsonConvert.DeserializeObject(内容);

您的JSON显示一个对象
结果
,对应于
根对象

但是您正在尝试反序列化
RootObject的数组(
IEnumerable

您应该使用它来反序列化JSON,如下所示:

JsonConvert.DeserializeObject<RootObject>(content);
JsonConvert.DeserializeObject(内容);

您的api返回名为结果的单个对象而不是一个您只需将其作为单个对象进行去搜索即可的集合

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

您的api返回名为结果的单个对象而不是一个您只需将其作为单个对象进行去搜索即可的集合

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

是的,它解决了这个问题。谢谢,它解决了这个问题。谢谢