从Web API反序列化JSON

本文关键字:JSON 反序列化 API Web | 更新日期: 2023-09-27 18:26:09

我有一个json格式的wikipediaapi。我想做一个动态编码,其中id号将及时出现。我想从api中提取信息,以显示网页的简短描述。我从jsonTocSharp中修改了一些json数据。但是在对url进行比较之后,我没有得到任何输出。这是我的代码示例。

 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;
      }
    }
 }

从Web API反序列化JSON

而不是

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

使用

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