简体   繁体   English

如何将响应转换为对象C#

[英]How to convert response to object c#

I have build a simple .net client that call a simple rest web service. 我已经构建了一个简单的.net客户端,该客户端调用了一个简单的Rest Web服务。 This works. 这可行。 The web services, return me this response: Web服务,返回此响应:

    <Reply>
       <errorCode>OK</errorCode>
       <errorDescription>d13f2197-ed6e-4997-a06d-4166ca52aa75</errorDescription>
       <ccdContent></ccdContent>
    </Reply>

Now I want to retrieve this response how to Object. 现在,我想检索此响应对象。 So I have create this class: 所以我创建了这个类:

class Response
{
     [DataMember(Name = "errorCode")]
    String errorCode { set; get; }
    [DataMember(Name = "errorDescription")]
    String errorDescription{ set; get; }
}

And this is the method that call a web service: 这是调用Web服务的方法:

public void writeSectionIntoCCD(String appID, String shardSecret, String userID, String password, String templateID)
{
    String currentTime = DateTime.Now.ToString("yyyyMMdd");
    String URL = "https://dokumentti.it/";
    var client = new RestClient(URL);
    string hash = CalculateMD5Hash(currentTime + shardSecret + password);
    RestRequest request = new RestRequest("resource/ccd/problems/" + userID + "/" + appID + "/" + currentTime + "/" 
    + hash + "?starttime=20140615&code=275498002&name=Respiratorytractinfection&system=2.16.840.1.113883.6.96&systemname=SNOMED&statuscode=55561003&statusname=Active&statussystem=2.16.840.1.113883.6.96&statussystemname=SNOMED", Method.POST);
    IRestResponse<Response> response = client.Execute<Response>(request);

}

but the object is null. 但该对象为null。

I am not sure but shouldn't you be doing something like: 我不确定,但您不应该这样做吗?

RestResponse<Response> response = client.Execute<Response>(request);
var errorCode = response.Data.errorCode;

I have found the solution, the problem are two: 我找到了解决方案,问题是两个:

I have insert this on the name of my class 我已在班级名称上插入

[DataContract(Name = "Reply")]

then I have insert public attribute on my class. 然后在班级上插入public属性。 So now I have this: 所以现在我有了这个:

[DataContract(Name = "Reply")]
public class Response
{
     [DataMember(Name = "errorCode")]
    public String errorCode { set; get; }
    [DataMember(Name = "errorDescription")]
    public String errorDescription{ set; get; }
}

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

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