首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从嵌套JSON - System.Collections.Generic.List`1[System.String]检索字符串

从嵌套JSON - System.Collections.Generic.List`1[System.String]检索字符串
EN

Stack Overflow用户
提问于 2017-12-20 00:52:14
回答 1查看 216关注 0票数 2

我使用一个JSON对象从不同的网站获取新闻标题。当我解压缩它时,我可以访问一个标题,但是当我试图循环这些项目并将它们添加到列表中以获得所有标题时,我会得到以下错误:

“System.Collections.Generic.List.Add(String)”的最佳重载方法匹配有一些无效的参数

我正在使用newtonsoft.json,然后将它变成一个动态对象,以便通过点符号访问。

JSON示例

代码语言:javascript
复制
{
    "status": "ok",
    "totalResults": 10,
    "articles": [{
        "source": {
            "id": "bloomberg",
            "name": "Bloomberg"
        },
        "author": null,
        "title": "Here’s Where the GOP Tax Plan Stands Right Now",
        "description": "The House is scheduled to vote Tuesday on the tax bill and Senate leaders intend to bring the measure up as soon as they get it.",
        "url": "http://www.bloomberg.com/news/articles/2017-12-19/house-plans-early-vote-democrats-plan-drama-tax-debate-update",
        "urlToImage": "https://assets.bwbx.io/images/users/iqjWHBFdfxIU/isN9fqUqLwpE/v0/1200x800.jpg",
        "publishedAt": "2017-12-19T09:00:00Z"
    }, {
        "source": {
            "id": "bloomberg",
            "name": "Bloomberg"
        },
        "author": "Dana Hull, Sarah Frier",
        "title": "Elon Musk Appears to Have Misfired Direct Message to Oculus CTO",
        "description": "Elon Musk appears to have just given his 16.7 million Twitter followers what he meant to send to the co-founder of the virtual-reality company Oculus: his phone number.",
        "url": "http://www.bloomberg.com/news/articles/2017-12-19/elon-musk-appears-to-have-misfired-direct-message-to-oculus-cto",
        "urlToImage": "https://assets.bwbx.io/images/users/iqjWHBFdfxIU/iqJHjveOq1j8/v1/1200x740.jpg",
        "publishedAt": "2017-12-19T21:41:36Z"
    }]
}

代码:

代码语言:javascript
复制
// Set base Website
var url = "https://newsapi.org/v2/top-headlines?sources=";
// Initiate Client
System.Net.WebClient client = new System.Net.WebClient();
// Get JSON Data
var json = client.DownloadString(url + site + apiKey);
// Convert to dynamic object
dynamic jOutput = JsonConvert.DeserializeObject(json);
// Get Articles
var articles = jOutput.articles;
//Declare List
List<String> titles = new List<String>();
foreach (var article in articles)
 {
   var  articleTitle = article.title;
   titles.Add(articleTitle);       
 }

string title = jOutput.articles[0].title;
//string txt = items[0].title;
EN

回答 1

Stack Overflow用户

发布于 2017-12-20 01:06:57

当反序列化为对象或动态类型时,Newtonsoft实际上将为您提供JValue项。

所以,这里的这一行:

代码语言:javascript
复制
var articleTitle = article.title;

articleTypeJValue型,而不是string型。因此,当您调用List<string>.Add(articleType) -它失败了,因为没有过载接受一个JValue

幸运的是,JValue有重载的cast运算符,它允许您展开实际值。只需将上面的行更改为:

代码语言:javascript
复制
string articleTitle = article.title;

足以让你的代码正常工作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47897343

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档