Reputation: 47
I am trying to parse a JSON string but I am getting an odd error:
Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Newtonsoft.Json.Linq.JProperty'
Would you check it and let me how I can get title
values from this JSON string!
[
{
"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 theamackersSuggesionResult = JArray.Parse(json);
foreach (JObject parsedObject in theamackersSuggesionResult.Children<JObject>())
{
foreach (JProperty parsedProperty in theamackersSuggesionResult)
{
string propertyName = parsedProperty.Name;
if (propertyName == "title")
{
MessageBox.Show(parsedProperty.Value.ToString());
KeywordSuggestionTable.Rows.Add(parsedProperty.Value.ToString());
KeywordResultDataGrid.Refresh();
}
}
}
Upvotes: 0
Views: 466
Reputation: 129777
The error is due to this line:
foreach (JProperty parsedProperty in theamackersSuggesionResult)
It should be:
foreach (JProperty parsedProperty in parsedObject.Properties())
Fiddle: https://dotnetfiddle.net/w9dC3n
Upvotes: 0
Reputation: 685
Also you can use this site -> http://json2csharp.com/
If you want to get model in C# of JSON data this site will be useful.
Upvotes: 0
Reputation: 2627
Try creating a model,
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; }
}
and use Newtonsoft to get the models from the json
var responseModels = JsonConvert.DeserializeObject<List<Example>>(json);
Then you can loop the responseModels
to get what you want.
Upvotes: 1