小编OhM*_*uFR的帖子

RestSharp POST对象为JSON

这是我的课:

public class PTList
{
    private String name;

    public PTList() { }
    public PTList(String name)
    {
        this.name = name;
    }


    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

}
Run Code Online (Sandbox Code Playgroud)

和我的RestSharp POST请求:

    protected static IRestResponse httpPost(String Uri, Object Data)
    {
        var client = new RestClient(baseURL);
        client.AddDefaultHeader("X-Authentication", AuthenticationManager.getAuthentication());
        client.AddDefaultHeader("Content-type", "application/json");
        var request = new RestRequest(Uri, Method.POST);

        request.RequestFormat = DataFormat.Json;

        request.AddJsonBody(Data);

        var response = client.Execute(request);
        return response;
    }
Run Code Online (Sandbox Code Playgroud)

当我使用具有良好URI和PTList对象的httpPost方法时,前端API会回答“名称”为null。我认为我的PTList对象未在API请求中序列化为有效JSON,但无法理解出了什么问题。

c# json restsharp

6
推荐指数
1
解决办法
9143
查看次数

标签 统计

c# ×1

json ×1

restsharp ×1