无法将类型'System.Collections.Generic.List <Apps.RootObject>'隐式转换为'Apps'

时间:2019-08-01 16:40:42

标签: c# asp.net-core .net-core

我正在尝试将JSON对象转换为C#列表。 JSON对象来自API。

我推荐了this https://stackoverflow.com/questions/22191167/convert-json-string-to-c-sharp-object-list

这是我获取数据的方式:

public async Task<IEnumerable<Apps>> getApps()
{
    var request = new HttpRequestMessage(HttpMethod.Get, "apps");
    var response = await _client_SB.SendAsync(request);
    var json = await response.Content.ReadAsStringAsync();
    Apps model = JsonConvert.DeserializeObject<List<Apps.RootObject>>(json);

    return model;
}

Apps类如下-

public class Apps
{
    public class First
    {
        public string href { get; set; }
    }

    public class Last
    {
        public string href { get; set; }
    }

    public class Next
    {
        public string href { get; set; }
    }

    public class Pagination
    {
        public int total_results { get; set; }
        public int total_pages { get; set; }
        public First first { get; set; }
        public Last last { get; set; }
        public Next next { get; set; }
        public object previous { get; set; }
    }

    public class Data
    {
        public List<object> buildpacks { get; set; }
        public string stack { get; set; }
    }

    public class Lifecycle
    {
        public string type { get; set; }
        public Data data { get; set; }
    }

    public class Data2
    {
        public string guid { get; set; }
    }

    public class Space
    {
        public Data2 data { get; set; }
    }

    public class Relationships
    {
        public Space space { get; set; }
    }

    public class Self
    {
        public string href { get; set; }
    }

    public class EnvironmentVariables
    {
        public string href { get; set; }
    }

    public class Space2
    {
        public string href { get; set; }
    }

    public class Processes
    {
        public string href { get; set; }
    }

    public class RouteMappings
    {
        public string href { get; set; }
    }

    public class Packages
    {
        public string href { get; set; }
    }

    public class CurrentDroplet
    {
        public string href { get; set; }
    }

    public class Droplets
    {
        public string href { get; set; }
    }

    public class Tasks
    {
        public string href { get; set; }
    }

    public class Start
    {
        public string href { get; set; }
        public string method { get; set; }
    }

    public class Stop
    {
        public string href { get; set; }
        public string method { get; set; }
    }

    public class Links
    {
        public Self self { get; set; }
        public EnvironmentVariables environment_variables { get; set; }
        public Space2 space { get; set; }
        public Processes processes { get; set; }
        public RouteMappings route_mappings { get; set; }
        public Packages packages { get; set; }
        public CurrentDroplet current_droplet { get; set; }
        public Droplets droplets { get; set; }
        public Tasks tasks { get; set; }
        public Start start { get; set; }
        public Stop stop { get; set; }
    }

    public class Labels
    {
        public string a { get; set; }
        public string description { get; set; }
    }

    public class Annotations
    {
    }

    public class Metadata
    {
        public Labels labels { get; set; }
        public Annotations annotations { get; set; }
    }

    public class Resource
    {
        public string guid { get; set; }
        public string name { get; set; }
        public string state { get; set; }
        public DateTime created_at { get; set; }
        public DateTime updated_at { get; set; }
        public Lifecycle lifecycle { get; set; }
        public Relationships relationships { get; set; }
        public Links links { get; set; }
        public Metadata metadata { get; set; }
    }

    public class RootObject
    {
        public Pagination pagination { get; set; }
        public List<Resource> resources { get; set; }
    }
}

Apps model = JsonConvert.DeserializeObject<List<Apps.RootObject>>(json);上的错误

  

无法隐式转换类型   “ System.Collections.Generic.List”到“ Apps”

有人可以帮我吗?我是.net的新手

更新:

我的Json对象 https://pastebin.com/wHXsVNa1

3 个答案:

答案 0 :(得分:1)

您显然误会了您定义的Apps类的实际组成。 它实际上具有0(零!)属性和多个内部类定义。

您实际的Apps类(根据您提供的JSON)是Apps.RootObject类。 如果我正确理解JSON的组成,则实际的应用程序位于资源数组中,因此它们用Resource类表示。

所以:

public async Task<IEnumerable<Apps.Resource>> getApps()
{
    var request = new HttpRequestMessage(HttpMethod.Get, "apps");
    var response = await _client_SB.SendAsync(request);
    var json = await response.Content.ReadAsStringAsync();
    // Get apps collection with pagination
    Apps.RootObject model = JsonConvert.DeserializeObject<Apps.RootObject>(json);
    // Return only apps
    return model.resources;
}

答案 1 :(得分:0)

目前尚不清楚您是要获取应用列表还是单个Apps对象。

假设您正在获取应用列表。

更改:

Apps model = JsonConvert.DeserializeObject<List<Apps.RootObject>>(json);

收件人:

var model = JsonConvert.DeserializeObject<Apps>(json);

答案 2 :(得分:-1)

Jojo,将“ models”更改为“ List<Apps.Rootobject>”后,您还需要将方法“ getapps”的返回类型也更改为“ Task <IEnumerable<Apps.Rootobject>>”。 / p>