维基百科的API JSON反序列化使用c# [英] Json deserialize from wikipedia api with c#

查看:149
本文介绍了维基百科的API JSON反序列化使用c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON格式的维基百科的API。现在,我想从这个API提取物的信息。我希望把它的动态任何维基百科的API。 [我的维基百科API] [1]。我得到了以下从jsontoCsharp

I have a wikipedia api with json format. Now I want to get the extract information from this api. I want to make it dynamic for any wikipedia api. [My wikipedia api][1]. I got following information from jsontoCsharp

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

      }

  }

}

我要做出这样应该是动态编码。这意味着ID 240912应及时变化。我尝试了很多。但不能得到一个满意的结果。

I want to make such a coding which should be dynamic. That means the id 240912 should be varied in time. I tried a lot. but cannot get a satisfied result.

推荐答案

所有的JSON对象基本上是字典。然而,它通常是有意义的代表它们中的C#类,假定的模式是恒定的。对于情况下,属性名称并不总是相同的,这是不可能写一个类来表示的对象;所以你多使用动态或字典

All JSON objects are essentially dictionaries. However, it usually makes sense to represent them as classes in C#, assuming the schema is constant. For cases where property names are not always the same, it's impossible to write a class to represent the object; so you much use dynamic or a dictionary

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

要获得提取物:

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="); ;

var responseJson = JsonConvert.DeserializeObject<RootObject>(response);
var firstKey = responseJson.query.pages.First().Key;
var extract = responseJson.query.pages[firstKey].extract;

这篇关于维基百科的API JSON反序列化使用c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆