C# 如何将这个JSON转换为C中的对象?

C# 如何将这个JSON转换为C中的对象?,c#,json,json.net,C#,Json,Json.net,两天来,我一直在试图理解如何将这个JSON移动到C中的对象。我阅读了很多主题,并尝试了几种方法来解决我的问题,但仍然没有解决它。 我的JSON看起来像是被裁剪过的 { "data": [{ "id": 5643793, "title": "It's a Title", "description": "It's a Descripti

两天来,我一直在试图理解如何将这个JSON移动到C中的对象。我阅读了很多主题,并尝试了几种方法来解决我的问题,但仍然没有解决它。 我的JSON看起来像是被裁剪过的

{
    "data": [{
        "id": 5643793,
        "title": "It's a Title",
        "description": "It's a Description.",
        "tags": "#tag1 #tag2 #tag3 #tag4",
        "source_url": "https:\/\/p.dw.com\/p\/3geny",
        "vote_count": 120,
        "bury_count": 17,
        "comments_count": 33,
        "related_count": 0,
        "date": "2020-08-10 09:43:32",
        "author": {
            "login": "lnwsk",
            "color": 2,
            "avatar": "https:\/\/www.api.page.com\/cdn\/c3397992\/lnwsk_MfQz8MEQb2,q150.jpg"
        },
        "preview": "https:\/\/www.api.page.com\/cdn\/c3397993\/link_1597045214DgzqxRGEmy2UlpPZwaWfhI,w104h74.jpg",
        "plus18": false,
        "status": "promoted",
        "can_vote": true,
        "is_hot": false
    }],
    "pagination": {
        "next": "https:\/\/api.page.com\/links\/promoted\/appkey\/X*******4y\/page\/2\/"
    }
}
正如你所看到的,这里的元素中有一个元素,比如author或pagination,比如pagination,我想去掉,这就是我遇到的最大问题

这是我的类,我有所有的代码来阅读API:

using Newtonsoft.JSON;

public class PageAPI
{
    public class Product
    {
        public string[] title { get; set; }
        public double[] description { get; set; }
        public string[] tags { get; set; }
        public string[] source_url { get; set; }
        public string[] vote_count { get; set; }
        public string[] bury_count { get; set; }
        public string[] comments_count { get; set; }
        public string[] related_count { get; set; }
        public string[] date { get; set; }
    }

    public async Task<Product> GetDataAsync()
    {
        string url = "https://api.page.com/";
        string apisign = "6*********c1fe49a23f19ad6b2";
        string requestParams = "links/promoted/appkey/X*******y";

        Product obj = null;

        // HTTP GET.  
        using (var client = new HttpClient())
        {
            // Setting Base address.  
            client.BaseAddress = new Uri(url);
            // Setting content type.  
            client.DefaultRequestHeaders.Add("apisign", apisign);
            // Initialization.  
            HttpResponseMessage response = new HttpResponseMessage();
            // HTTP GET  
            response = await client.GetAsync(requestParams).ConfigureAwait(false);
            // Verification  
            if (response.IsSuccessStatusCode)
            {
                try
                {
                    // Reading Response.  
                    string result = response.Content.ReadAsStringAsync().Result;
                    obj = JsonConvert.DeserializeObject<Product>(result);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    MessageBox.Show(ex.Message);
                }

            }
            else
            {
                obj = null;
            }
        }
        return obj;
    }
}
而且。。。这不起作用-在label1.Text=data.title[0]上;我得到错误PageAPI.Product.title.get返回null
谢谢你的帮助

缺少具有数据和分页属性的根类。创建根类并对其进行反序列化,然后获取所需的数据。另外,您的产品类将只有字符串。。不是字符串[]

public class RootObject
{
    public List<Product> data { get; set; }
}

public class Product
{
    public string title { get; set; }
    public double description { get; set; }
    public string tags { get; set; }
    public string source_url { get; set; }
    public string vote_count { get; set; }
    public string bury_count { get; set; }
    public string comments_count { get; set; }
    public string related_count { get; set; }
    public string date { get; set; }
}

// and deserialize it 
var rootObj = JsonConvert.DeserializeObject<RootObject>(result);
obj = rootObj.data.FirstOrDefault();
当您反序列化json时,您可以使用

Console.WriteLine(rootObj.pagination.next); // prints the url
如何显示所有产品名称

这就是如何获取数据对象中所有标题的列表

foreach (var product in rootObj.data) 
{
    Console.WriteLine(product.title);
    Console.WriteLine(product.description);
    Console.WriteLine(product.vote_count); // etc.
}

// Or you can create a list of all the titles from the rootObj using LINQ
List<string> allTitles = rootObj.data.Select(x => x.title).ToList();

我不知道你打算用你得到的数据做什么。。。所以我不知道该如何解释这件事。。但上面的示例应该让您了解如何迭代数据对象中的所有产品。

您缺少具有数据和分页属性的根类。创建根类并对其进行反序列化,然后获取所需的数据。另外,您的产品类将只有字符串。。不是字符串[]

public class RootObject
{
    public List<Product> data { get; set; }
}

public class Product
{
    public string title { get; set; }
    public double description { get; set; }
    public string tags { get; set; }
    public string source_url { get; set; }
    public string vote_count { get; set; }
    public string bury_count { get; set; }
    public string comments_count { get; set; }
    public string related_count { get; set; }
    public string date { get; set; }
}

// and deserialize it 
var rootObj = JsonConvert.DeserializeObject<RootObject>(result);
obj = rootObj.data.FirstOrDefault();
foreach (var product in rootObj.data) 
{
    Console.WriteLine(product.title);
    Console.WriteLine(product.description);
    Console.WriteLine(product.vote_count); // etc.
}

// Or you can create a list of all the titles from the rootObj using LINQ
List<string> allTitles = rootObj.data.Select(x => x.title).ToList();
当您反序列化json时,您可以使用

Console.WriteLine(rootObj.pagination.next); // prints the url
如何显示所有产品名称

这就是如何获取数据对象中所有标题的列表

foreach (var product in rootObj.data) 
{
    Console.WriteLine(product.title);
    Console.WriteLine(product.description);
    Console.WriteLine(product.vote_count); // etc.
}

// Or you can create a list of all the titles from the rootObj using LINQ
List<string> allTitles = rootObj.data.Select(x => x.title).ToList();

我不知道你打算用你得到的数据做什么。。。所以我不知道该如何解释这件事。。但上面的例子应该让您了解如何迭代数据对象中的所有产品。

可能在某个地方描述了如何传输所有数据?您给出了一个如何使用单个元素(JSON中的ie)实现这一点的示例。我有更多的title、description、date等元素,我希望获得它们,而不仅仅是第一个元素。您的示例最有效-谢谢-但我想从JSON获取所有元素,所以我使用了字符串[]。@thewolf1119请参阅本文中的更新。FirstOrDefault不仅检索标题。。它检索数据数组中包含标题、描述、标记等的第一个对象。。。等等。可能在某个地方描述了如何传输所有数据?您给出了一个如何使用单个元素(JSON中的ie)实现这一点的示例。我有更多的title、description、date等元素,我希望获得它们,而不仅仅是第一个元素。您的示例最有效-谢谢-但我想从JSON获取所有元素,所以我使用了字符串[]。@thewolf1119请参阅本文中的更新。FirstOrDefault不仅检索标题。。它检索数据数组中包含标题、描述、标记等的第一个对象。。。等
foreach (var product in rootObj.data) 
{
    Console.WriteLine(product.title);
    Console.WriteLine(product.description);
    Console.WriteLine(product.vote_count); // etc.
}

// Or you can create a list of all the titles from the rootObj using LINQ
List<string> allTitles = rootObj.data.Select(x => x.title).ToList();