解析Json字符串C#

解析Json字符串C#,c#,json,winforms,json.net,C#,Json,Winforms,Json.net,我试图解析JSON字符串,但遇到一个奇怪的错误: 无法将“Newtonsoft.Json.Linq.JObject”类型的对象强制转换为“Newtonsoft.Json.Linq.JProperty”类型 请检查一下,然后告诉我如何从这个JSON字符串中获取title值 [ { "id":"14962106", "title":"Why is Yahoo called Yahoo", "link":"http:\/\/www.answers.com\/Q

我试图解析JSON字符串,但遇到一个奇怪的错误:

无法将“Newtonsoft.Json.Linq.JObject”类型的对象强制转换为“Newtonsoft.Json.Linq.JProperty”类型

请检查一下,然后告诉我如何从这个JSON字符串中获取
title

[
   {
      "id":"14962106",
      "title":"Why is Yahoo called Yahoo",
      "link":"http:\/\/www.answers.com\/Q\/Why_is_Yahoo_called_Yahoo",
      "is_answered":true
   },
   {
      "id":"65001091",
      "title":"Connecting yahoo IM to yahoo",
      "link":"http:\/\/www.answers.com\/Q\/Connecting_yahoo_IM_to_yahoo",
      "is_answered":true
   },
   {
      "id":"45440021",
      "title":"Why doesn't Yahoo recognize my Yahoo account",
      "link":"http:\/\/www.answers.com\/Q\/Why_doesn%27t_Yahoo_recognize_my_Yahoo_account",
      "is_answered":true
   },
   {
      "id":"264383657",
      "title":"How is Yahoo different from Yahoo Mail",
      "link":"http:\/\/www.answers.com\/Q\/How_is_Yahoo_different_from_Yahoo_Mail",
      "is_answered":true
   },
   {
      "id":"11230021",
      "title":"Does Yahoo block email",
      "link":"http:\/\/www.answers.com\/Q\/Does_Yahoo_block_email",
      "is_answered":true
   },
   {
      "id":"11230461",
      "title":"Is yahoo answers gone",
      "link":"http:\/\/www.answers.com\/Q\/Is_yahoo_answers_gone",
      "is_answered":true
   },
   {
      "id":"12097857",
      "title":"What is Yahoo BrowserPlus",
      "link":"http:\/\/www.answers.com\/Q\/What_is_Yahoo_BrowserPlus",
      "is_answered":true
   },
   {
      "id":"100301924",
      "title":"Is yahoo answers useful",
      "link":"http:\/\/www.answers.com\/Q\/Is_yahoo_answers_useful",
      "is_answered":true
   },
   {
      "id":"107057666",
      "title":"Are yahoo emails free",
      "link":"http:\/\/www.answers.com\/Q\/Are_yahoo_emails_free",
      "is_answered":true
   },
   {
      "id":"107858033",
      "title":"Is yahoo games free",
      "link":"http:\/\/www.answers.com\/Q\/Is_yahoo_games_free",
      "is_answered":true
   }
]

JArray theamackersuggesionresult=JArray.Parse(json);
foreach(MackerSuggestionResult.Children()中的JObject parsedObject)
{
foreach(MackerSuggestionResult中的JPProperty parsedProperty)
{
string propertyName=parsedProperty.Name;
如果(propertyName==“标题”)
{
Show(parsedProperty.Value.ToString());
关键字SuggestionTable.Rows.Add(parsedProperty.Value.ToString());
关键字ResultDataGrid.Refresh();
}
}
}
尝试创建一个模型

public class Example
    {
        [JsonProperty("id")]
        public string id { get; set; }

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

        [JsonProperty("link")]
        public string link { get; set; }

        [JsonProperty("is_answered")]
        public bool is_answered { get; set; }
    }
并使用Newtonsoft从json获取模型

var responseModels = JsonConvert.DeserializeObject<List<Example>>(json);
var responseModels=JsonConvert.DeserializeObject(json);

然后,您可以循环
响应模型
以获得所需信息。

您也可以使用此网站->


如果您想获取JSON数据的C#形式的模型,此站点将非常有用。

错误是由以下行引起的:

foreach (JProperty parsedProperty in theamackersSuggesionResult)
应该是:

foreach (JProperty parsedProperty in parsedObject.Properties())

Fiddle:

为什么不为json创建一个模型,并使用Newtonsoft?
foreach (JProperty parsedProperty in parsedObject.Properties())