0

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();
        }
    }
}
1
  • 2
    why don't you create a model for the json, and use Newtonsoft? Commented Nov 16, 2018 at 11:28

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.