繁体   English   中英

JSON object string in C#, How to get article URL from the following JSON object in c#

[英]JSON object string in C#, How to get article URL from the following JSON object in c#

我正在尝试使用 NewsAPI 获取相关文章,但返回类型为 JSON object。 我以前从未使用过 JSON 对象,我查找的每个答案似乎都针对每个特定的 JSON object 进行了个性化,我真的不明白如何应用到我的。

c# 中的以下代码...

using System.Net;

var url = "https://newsapi.org/v2/top-headlines?" +
      "country=us&" +
      "apiKey=*mySpecificKey*";

var json = new WebClient().DownloadString(url);

给我一个看起来像这样的 JSON object...

{

"status": "ok",
"totalResults": 38,
-
"articles": [
    -
    {
        -
        "source": {
            "id": "cnn",
            "name": "CNN"
        },
        "author": "Frederik Pleitgen and Mary Ilyushina, CNN",
        "title": "US is out of the picture in Syria-Turkey crisis. Putin now owns this mess - CNN",
        "description": "As US President Donald Trump hailed the agreement his administration negotiated with the Turks for northern Syria as \"a great day for civilization,\" the Turks quickly dumped cold water over the White House's euphoria, refusing to even call the deal a ceasefir…",
        "url": "https://www.cnn.com/2019/10/20/middleeast/putin-now-owns-this-mess-intl/index.html",
        "urlToImage": "https://cdn.cnn.com/cnnnext/dam/assets/190827100831-russia-turkey-aviation-politics-diplomacy-super-tease.jpg",
        "publishedAt": "2019-10-20T05:16:00Z",
        "content": "Moscow (CNN)As US President Donald Trump hailed the agreement his administration negotiated with the Turks for northern Syria as \"a great day for civilization,\" the Turks quickly dumped cold water over the White House's euphoria, refusing to even call the dea… [+6294 chars]"
    },
    -
    {
        -
        "source": {
            "id": null,
            "name": "Bbc.com"

    ... and so on.

而且我需要访问每篇文章的url。 作为 JSON object 的极新手用户,我将如何从 go 中获取 ZE6B391A8D2C4D5570 的 ZE6B391A8D2C4D85708 的每篇文章? 请尽可能详尽地解释。 提前感谢您的任何帮助!

为此,您需要使用众所周知的 lib Newtonsoft.Json。 在这里,我通过 3 个类创建了 JSON 结果的整体 model。 主要的 class 是 Result.json,其中包含文章和来源。 在请求并获得 json 后,您必须对它进行反序列化。 通过 LINQ 我得到了所有的网址。

看看这段代码。 我已经测试过了,它可以工作。

    public class Source
{
    [JsonProperty(PropertyName = "id")]
    public string Id { get; set; }

    [JsonProperty(PropertyName = "name")]
    public string Name { get; set; }
}


public class Article
{
    [JsonProperty(PropertyName = "author")]
    public string Author { get; set; }

    [JsonProperty(PropertyName = "title")]
    public string Title { get; set; }

    [JsonProperty(PropertyName = "description")]
    public string Description { get; set; }

    [JsonProperty(PropertyName = "url")]
    public string Url { get; set; }

    [JsonProperty(PropertyName = "urlToImage")]
    public string UtlToImage { get; set; }

    [JsonProperty(PropertyName = "publishedAt")]
    public string PublishedAt { get; set; }

    [JsonProperty(PropertyName = "content")]
    public string Content { get;set; }
}

public class  ResultJson
{

    public List<Source> Sources { get; set; }

    public List<Article> Articles { get; set; }
}

public class Programm 
{
        var json = new WebClient().DownloadString(url);

        var articles = JsonConvert.DeserializeObject<ResultJson>(json).Articles.ToList();

        List<string> allUrls = new List<string>();

        allUrls = articles.Select(u => u.Url).ToList();
}

暂无
暂无

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

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