首页 > 解决方案 > Get 方法在使用 C# 的 Restsharp 中不起作用

问题描述

获取方法不起作用...下面的代码使用restsharp从json显示富文本框中的所有名称或其他属性...没有错误但没有输出帮助我解决这个问题...

var client = new RestClient("http://www.jsongenerator.com/api/json/get/cfBwXjwjci?indent=2");
var request = new RestRequest(Method.GET);
var queryResult = client.Execute<List<Detail>>(request).Data;
foreach (var rl in queryResult)
     {
        richTextBox1.Text = rl.name;
     }

public class Detail
    {
        public string city { get; set; }
        public int id { get; set; }
        public string Blood { get; set; }
        public string name { get; set; }
    }

这是json

{
  "Details": [
    {
      "city": "Londan", 
      "id": 1, 
      "Blood": "O+", 
      "name": "Nicolas"
    }, 
    {
      "city": "USA", 
      "id": 2, 
      "Blood": "A+", 
      "name": "Jhon"
    }, 
    {
      "city": "India", 
      "id": 3, 
      "Blood": "B-", 
      "name": "Shiva"
    }
  ]
}

标签: c#jsonrestrestsharp

解决方案


我看到两个问题:

1)“ http://www.jsongenerator.com/api/json/get/cfBwXjwjci?indent=2 ” - 不起作用

2) 如果您提供了正确的 JSON 示例,那么您应该在此处使用“RootObject”:

client.Execute<List<Detail>>(request).Data;

推荐阅读