在列表或网格视图中进行锐利的数据解析和查看

rest sharp data parse and view in list or grid view

我正在使用 RestSharp 在 C# 中启动 REST API。我有一个 form1 class:

private void Form1_Load(object sender, EventArgs e)
    {
         var client = new RestClient("http://189.168.1.0/res_app/");
        var request = new RestRequest("/get_category", Method.POST);
        //request.AddParameter("application/json; charset=utf-8", "");
        request.RequestFormat = DataFormat.Json;

        try
        {
            client.ExecuteAsync(request, response =>
            {
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    //OK
                    MessageBox.Show(response.Content);
                }
                else
                {
                    //NOK
                    MessageBox.Show("FAIL");
                }
            });
        }
        catch (Exception error)
        {
            //log
        }

    }
}

消息框中 RestSharp 的输出

{
    "data": "successful",
    "total_user": 8,
    "unverified_count": 2,
    "unverified": [
        {
            "user_id": "189",
            "user_name": "mahendra rajput"
        },
        {
            "user_id": "200",
            "user_name": "gdfhdfhdh"
        }
    ],
    "verified_count": 6,
    "verified": [
        {
            "user_id": "184",
            "user_name": "Shivi R Jaiswal",
            "money": "0"
        },
        {
            "user_id": "182",
            "user_name": "Sankalp Kanungo",
            "money": "0"
        },
        {
            "user_id": "181",
            "user_name": "rakesh",
            "money": "0"
        },
        {
            "user_id": "191",
            "user_name": "Neha Shrivastav",
            "money": "0"
        },
        {
            "user_id": "180",
            "user_name": "mahendra rajput",
            "money": "0"
        },
        {
            "user_id": "196",
            "user_name": "Kailash Chouhan",
            "money": "0"
        }
    ],
    "deactivate_count": 9,
    "deactivate": [
        {
            "user_id": "186",
            "user_name": "Yashika Shivhare",
            "money": "0"
        },
        {
            "user_id": "199",
            "user_name": "Shraddha Rai",
            "money": "0"
        },
        {
            "user_id": "198",
            "user_name": "Shivi Rai",
            "money": "0"
        },
        {
            "user_id": "193",
            "user_name": "Ratufa Dev",
            "money": "0"
        },
        {
            "user_id": "197",
            "user_name": "RakeshKumar",
            "money": "0"
        },
        {
            "user_id": "201",
            "user_name": "Rakesh kumar",
            "money": "0"
        },
        {
            "user_id": "202",
            "user_name": "Rakesh Kumar",
            "money": "0"
        },
        {
            "user_id": "195",
            "user_name": "new",
            "money": "0"
        },
        {
            "user_id": "194",
            "user_name": "Mahesh Sahu",
            "money": "0"
        }
    ]
}

如何在网格或列表视图中显示其数据?

首先你需要反序列化 JSON 到模型中。模型是表示 JSON 字符串的对象(请参见下面的示例)。 对于反序列化,您可以使用默认的 .Net JavaScriptSerializer or third party library like Newtonsoft.Json(您可以使用 NuGet 包管理器下载第三方库(右键单击项目 => 管理 NuGet 包 => 搜索你想要的))。反序列化后,您可以将集合绑定到网格/列表视图,如下所示:

    GridView_List.DataSource = response.VerifiedUsers;
    GridView_List.DataBind();

这是使用 Newtonsfoft.Json

的模型和数据绑定示例

型号:

// Response model
public class DataResponse
{
    [JsonProperty("data")]
    public string Data { get; set; }

    [JsonProperty("total_user")]
    public int TotalUsersCount { get; set; }

    [JsonProperty("unverified_count")]
    public int UnverifiedCount { get; set; }

    [JsonProperty("verified_count")]
    public int VerifiedCount { get; set; }

    [JsonProperty("deactivate_count")]
    public int DeactivateCount { get; set; }

    [JsonProperty("verified")]
    public List<UserInfo> VerifiedUsers { get; set; }

    [JsonProperty("unverified")]
    public List<UserInfo> UnverifiedUsers { get; set; }

    [JsonProperty("deactivate")]
    public List<UserInfo> DeactivateUsers { get; set; }
}

// User model
public class UserInfo
{
    [JsonProperty("user_id")]
    public int Id { get; set; }

    [JsonProperty("user_name")]
    public string Name { get; set; }

    [JsonProperty("money")]
    public double? Money { get; set; }
}

反序列化和绑定:

    public BindData(string json)
    {
        var response = Newtonsoft.Json.JsonConvert.DeserializeObject<DataResponse>(json);
        GridView_List.DataSource = response.VerifiedUsers;
        GridView_List.DataBind();
    }