简体   繁体   English

RestSharp(C#)-将JSON对象反序列化为NULL

[英]RestSharp (C#) - Deserialize JSON Objects to NULL

I'm trying to understand REST by utilizing RestSharp in my application. 我试图通过在应用程序中利用RestSharp来了解REST。

Here is a bit of JSON that my client is returning: 这是我的客户返回的一些JSON:

{
   "result" : {
      "object_type" : "session",
      "user_id" : "FEE3CBD4-5D35-11E3-A42A-606A40E381E5",
      "object_name" : "Session",
      "id" : "2F2968B6-5D37-11E3-89F4-5D6A40E381E5"
   }
}

Here is my class object: 这是我的班级对象:

public class TGCResult : IDeserializer
{
    public string object_type {get; set;}
    public string user_id { get; set; }
    public string object_name { get; set; }
    public string id { get; set; }

    public TGCResult()
    {
    }

    public override string ToString()
    {
        return "object_type = " + object_type + "\nuser_id = " + user_id + "\nobject_name = " + object_name + "\nid = " + id;
    }
}

And here is the code in which I am retrieving the JSON and attempting to deserialize: 这是我检索JSON并尝试反序列化的代码:

var client = new RestClient("https://www.xxxxxxxxx.com");

var request = new RestRequest("/api/session", Method.POST);
request.AddParameter("username", "JSventoraGD");
request.AddParameter("password", "xxxxxxxxxxxxxxxx");
request.AddParameter("api_key_id", "xxxxxxxxxxxxxxxxxxx");
request.RequestFormat = DataFormat.Json;

var asyncHandle = client.ExecuteAsync<TGCResult>(request, response =>
{
    TxtTest.Text = response.Data.ToString();
});

asyncHandle.Abort();

When doing this, I can see the data is returned to my application correctly, but the Data in my response always has NULL values. 这样做时,我可以看到数据已正确返回到我的应用程序,但是响应中的数据始终具有NULL值。 Any ideas on why this might be happening? 为什么会发生这种情况的任何想法? Am I supposed to manually deserialize the JSON given the content? 给定内容,我是否应该手动反序列化JSON? The samples I've seen online have a very similar setup to mine, I'm lost... any help is greatly appreciated! 我在网上看到的样本与我的样本具有非常相似的设置,我迷路了……非常感谢您的帮助!

Try adding 尝试添加

request.RootElement = "result";

I have just spent a couple of hours trying to get a simple GET request to parse anything but null, setting RootElement helped me. 我花了几个小时试图获取一个简单的GET请求来解析除null以外的任何内容,设置RootElement可以帮助我。 Bit surprised i needed it since you set your type class several other places in the code. 有点惊讶我需要它,因为您在代码中的其他几个地方都设置了类型类。

var asyncHandle=client.ExecuteAsync(request,response=>{    
   String res=response.Content;
   T obj = Newtonsoft.Json.JsonConvert.DeserializeObject<T>(res); 
});

Try using it like this 尝试像这样使用

var asyncHandle = client.ExecuteAsync(request, response =>
{
    var json = response.Content;
    // deserialize to TGCResult
});

This works fine for me 这对我来说很好

It seems you need to have another class as your Data type. 看来您需要另一个类作为您的数据类型。

public class TGCResultContainer
{
    public TGCResult result { get; set; }
}

Also, your TGCResult class does not need to implement IDeserializer. 另外,您的TGCResult类不需要实现IDeserializer。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM