我对这个职位道歉,因为它可能看起来平庸的一些人。不过,我想了解GET API的工作,遗憾的是,不知何故,我还没有找到一个可访问的教程。从实例来最好的学习方法,任何人都可以告诉我怎么弄的最简单的方法从名称标签值是多少?可以达到文本框。
在XML:
https://bdl.stat.gov.pl/api/v1/subjects?lang=pl&format=xml
在JSON:
https://bdl.stat.gov.pl/api/v1/subjects?lang=pl&format=json
码
public class Result
{
public string id { get; set; }
public string name { get; set; }
public bool hasVariables { get; set; }
public List<string> children { get; set; }
public string levels { get; set; }
}
private void button1_Click(object sender, EventArgs e)
{
using (WebClient wc = new WebClient())
{
wc.Encoding = System.Text.Encoding.UTF8;
var json = wc.DownloadString("https://bdl.stat.gov.pl/api/v1/subjects?lang=pl&format=json");
Result result = JsonConvert.DeserializeObject<Result>(json);
richTextBox1.Text = result.name;
}
}
预先感谢您的帮助。
你是为了让反序列化正确的JSON字符串缺少各种类。尝试这样的:
public class Results
{
public string id { get; set; }
public string name { get; set; }
public bool hasVariables { get; set; }
public List<string> children { get; set; }
public string levels { get; set; }
}
public class Links
{
public string first { get; set; }
public string self { get; set; }
public string next { get; set; }
public string last { get; set; }
}
public class JsonObject
{
public int totalRecords { get; set; }
public int page { get; set; }
public int pageSize { get; set; }
public Links links { get; set; }
public List<Results> results { get; set; }
}
然后用这样的:
using (WebClient wc = new WebClient())
{
var json = wc.DownloadString("https://bdl.stat.gov.pl/api/v1/subjects?lang=pl&format=json");
JsonObject result = JsonConvert.DeserializeObject<JsonObject>(json);
foreach (var res in result.results)
{
MessageBox.Show(res.name);
}
}