user3193794
user3193794

Reputation: 105

Deserialize JSON from Web API

I have an wikipedia api with json format.I want to make a dynamib´c coding in where id number will be chanced in time. from the api I want to get extract information to show the short description of web page. I modified my json data a little bit from jsonTocSharp. but after parisng the url I am not getting any output. here is my code sample.

 namespace Json_deserialize
 {
  public class pageval
 {
   public int pageid { get; set; }
   public int ns { get; set; }
   public string title { get; set; }
   public string extract { get; set; }
  }


   public class Query
  {
    public Dictionary<string, pageval> pages { get; set; }
   }

   public class Limits
 {
    public int extracts { get; set; }
  }

  public class RootObject
 {
   public string batchcomplete { get; set; }
   public Query query { get; set; }
   public Limits limits { get; set; }
 }

class Short_text
{
  public static RichTextBox txt1 = new RichTextBox();
  public static void shortText()
  {
     using (WebClient wc = new WebClient())
     {
        var client = new WebClient();
        var response = client.DownloadString("https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exlimit=max&explaintext&exintro&titles=Neuschwanstein%20Castle&redirects="); ;

        pageval m = JsonConvert.DeserializeObject<pageval>(response);

        string result = m.extract;

        txt1.Text = result;
      }

    }

 }

Upvotes: 0

Views: 1748

Answers (1)

Anik Saha
Anik Saha

Reputation: 4494

instead of

   pageval m = JsonConvert.DeserializeObject<pageval>(response);

use

RootObject m = JsonConvert.DeserializeObject<RootObject>(response);

Upvotes: 2

Related Questions