Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从嵌套的JSON-System.Collections.Generic.List`1[System.String]中检索字符串_C#_Json - Fatal编程技术网

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

C# 从嵌套的JSON-System.Collections.Generic.List`1[System.String]中检索字符串,c#,json,C#,Json,我使用JSON对象从各种网站获取新闻标题。当我解包时,我可以访问一个标题,但当我尝试循环项目并将其添加到列表中以获取所有标题时,我得到以下错误: 与“System.Collections.Generic.List.Add(string)”匹配的最佳重载方法具有一些无效参数 我正在使用newtonsoft.json,然后将它变成一个动态对象,以便通过点表示法获得访问权限 JSON示例 { "status": "ok", "totalResults": 10, "articl

我使用JSON对象从各种网站获取新闻标题。当我解包时,我可以访问一个标题,但当我尝试循环项目并将其添加到列表中以获取所有标题时,我得到以下错误:

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

我正在使用newtonsoft.json,然后将它变成一个动态对象,以便通过点表示法获得访问权限

JSON示例

{
    "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"
    }]
}
代码:

//设置基本网站
变量url=”https://newsapi.org/v2/top-headlines?sources=";
//启动客户端
System.Net.WebClient client=新系统.Net.WebClient();
//获取JSON数据
var json=client.DownloadString(url+site+apiKey);
//转换为动态对象
动态jOutput=JsonConvert.DeserializeObject(json);
//获取文章
var articles=jOutput.articles;
//申报表
列表标题=新列表();
foreach(条款中的var条款)
{
var articleTitle=article.title;
标题。添加(文章标题);
}
字符串title=jOutput.articles[0]。title;
//字符串txt=项目[0]。标题;

Newtonsoft将在反序列化为对象或动态类型时实际为您提供
JValue

那么,这一行:

var articleTitle = article.title;
articleType
属于
JValue
类型,而不是
string
。因此,当您调用
List.Add(articleType)
时,它会失败,因为没有重载接受
JValue

幸运的是,
JValue
具有重载的强制转换运算符,允许您展开实际值。只需将上述行更改为:

string articleTitle = article.title;

足够让您的代码正常工作。

谢谢您的回复。我仍然没有得到列表中的值,而是得到了“System.Collections.Generic.list`1[System.String]”。有解决方法吗?@phil.pidis您确定上面的JSON正是您要解析的内容吗?因为您的结果将暗示title实际上是一个字符串数组,而不是一个字符串。或者,你从哪里得到的文本?如果您正在尝试
控制台.WriteLine(标题)
,这是意料之中的。抱歉@Rob正在编辑。上面的意思是“是的,它是相同的JSON”。不,我没有做Console.WriteLine(标题),但我正在尝试在另一个平台上显示数据,这可能是问题所在!谢谢你现在就检查这个,我一直在排除故障,但我仍然得到同样的结果。无法获取列表中的值..@phil.pidis调试时列表中有什么?在进入表示层之前。在
string title=jOutput.articles[0]上放置断点带有我的更改,并检查列表,应该具有您期望的值。我刚刚用您的JSON blob在本地测试了它。
string articleTitle = article.title;