简体   繁体   English

如何将JSON反序列化到此POCO类中?

[英]How can I deserialize JSON into this POCO class?

ERROR: 错误:

Cannot implicitly convert type 'UserItem' to 'RootObject' 无法将类型'UserItem'隐式转换为'RootObject'

How can I deserialize JSON into this POCO class? 如何将JSON反序列化到此POCO类中?

I'm simply trying to deserilize json data into C# custom poco class as shown below and here is what i have done so far; 我只是试图将json数据反序列化为C#自定义poco类,如下所示,这是我到目前为止所做的;

public static UserItem DownloadJSONString(string urlJson)
{
    using (WebClient wc = new WebClient())
    {
        var json = wc.DownloadString(urlJson);
        UserItem userItems = JsonConvert.DeserializeObject<RootObject>(json);

        return userItems;
    }            
}

I'm kind of stuck here 我有点卡在这里

here my Json: 这是我的杰森:

{
    "meta":
    {
        "status":200,
        "resultSet":
        {
            "id":"05"
        },
        "pagination":
        {
            "count":2,
            "pageNum":1,
            "pageSize":2
        }
    },
    "results":   
    {
        "id":0,
        "name":
        "title",
        "items":
        [
            {
                "id":0,
                "name":"English",
                "title":"English",
            },
            {
                "id":0,
                "name":"Spanish",
                "title":"Spanish;",
            }
        ]
    }
}

here is my json object (generate from json to c# class) 这是我的json对象(从json生成为c#类)

public class ResultSet
{
    public string id { get; set; }
}

public class Pagination
{
    public int count { get; set; }
    public int pageNum { get; set; }
    public int pageSize { get; set; }
}

public class Meta
{
    public int status { get; set; }
    public ResultSet resultSet { get; set; }
    public Pagination pagination { get; set; }
}

public class Item
{
    public int id { get; set; }
    public string name { get; set; }
    public string title { get; set; }
}

public class Results
{
    public int id { get; set; }
    public string name { get; set; }
    public List<Item> items { get; set; }
}

public class RootObject
{
    public Meta meta { get; set; }
    public Results results { get; set; }
}

Here is my simple UserItem POCO class 这是我简单的UserItem POCO类

public class UserItem 
{
    public int id { get; set; }
    public string name { get; set; }
    public string title { get; set; }
}

Here you go: 干得好:

var root = JsonConvert.DeserializeObject<RootObject>(json);
var userItem = root.results.items
                           .Select(i => new UserItem
                           {
                               id = i.id,
                               name = i.name,
                               title = i.title
                           }).FirstOrDefault();

return userItem;

As you already know, you can't just convert the Item class into a UserItem , but you can build one. 如您所知,您不仅可以将Item类转换为UserItem ,还可以构建一个。 One thing to note here is that obviously if you only want to return one you'll have to just grab one. 这里要注意的一件事是,显然,如果您只想退回一个,则只需要抓住一个即可。 Here I've grabbed the first or default. 在这里,我抓住了第一个或默认值。 The default would be null if the list were empty for example. 例如,如果列表为空,则默认null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM