Parsing Json string C#



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








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();












share|improve this question



















  • 2





    why don't you create a model for the json, and use Newtonsoft?

    – Akbar Badhusha
    Nov 16 '18 at 11:28

















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();












share|improve this question



















  • 2





    why don't you create a model for the json, and use Newtonsoft?

    – Akbar Badhusha
    Nov 16 '18 at 11:28













0












0








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();












share|improve this question
















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();









c# json winforms json.net






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 11:51









Andrew Morton

16k63553




16k63553










asked Nov 16 '18 at 11:25









J. MarkJ. Mark

174




174







  • 2





    why don't you create a model for the json, and use Newtonsoft?

    – Akbar Badhusha
    Nov 16 '18 at 11:28












  • 2





    why don't you create a model for the json, and use Newtonsoft?

    – Akbar Badhusha
    Nov 16 '18 at 11:28







2




2





why don't you create a model for the json, and use Newtonsoft?

– Akbar Badhusha
Nov 16 '18 at 11:28





why don't you create a model for the json, and use Newtonsoft?

– Akbar Badhusha
Nov 16 '18 at 11:28












3 Answers
3






active

oldest

votes


















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.






share|improve this answer


















  • 1





    Perfect ! thanks

    – J. Mark
    Nov 16 '18 at 11:42


















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.






share|improve this answer






























    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






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53336914%2fparsing-json-string-c-sharp%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      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.






      share|improve this answer


















      • 1





        Perfect ! thanks

        – J. Mark
        Nov 16 '18 at 11:42















      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.






      share|improve this answer


















      • 1





        Perfect ! thanks

        – J. Mark
        Nov 16 '18 at 11:42













      1












      1








      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.






      share|improve this answer













      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.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 16 '18 at 11:32









      Akbar BadhushaAkbar Badhusha

      842918




      842918







      • 1





        Perfect ! thanks

        – J. Mark
        Nov 16 '18 at 11:42












      • 1





        Perfect ! thanks

        – J. Mark
        Nov 16 '18 at 11:42







      1




      1





      Perfect ! thanks

      – J. Mark
      Nov 16 '18 at 11:42





      Perfect ! thanks

      – J. Mark
      Nov 16 '18 at 11:42













      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.






      share|improve this answer



























        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.






        share|improve this answer

























          0












          0








          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.






          share|improve this answer













          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 16 '18 at 12:11









          MateuszMateusz

          76210




          76210





















              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






              share|improve this answer



























                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






                share|improve this answer

























                  0












                  0








                  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






                  share|improve this answer













                  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







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 16 '18 at 18:31









                  Brian RogersBrian Rogers

                  78.5k18197211




                  78.5k18197211



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53336914%2fparsing-json-string-c-sharp%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Top Tejano songwriter Luis Silva dead of heart attack at 64

                      政党

                      天津地下鉄3号線