reading HttpwebResponse json response, C#

后端 未结 3 804
伪装坚强ぢ
伪装坚强ぢ 2020-12-25 10:55

In one of my apps, I am getting the response from a webrequest. The service is Restful service and will return a result similar to the JSON format below:

{
          


        
3条回答
  •  甜味超标
    2020-12-25 11:45

    I'd use RestSharp - https://github.com/restsharp/RestSharp

    Create class to deserialize to:

    public class MyObject {
        public string Id { get; set; }
        public string Text { get; set; }
        ...
    }
    

    And the code to get that object:

    RestClient client = new RestClient("http://whatever.com");
    RestRequest request = new RestRequest("path/to/object");
    request.AddParameter("id", "123");
    
    // The above code will make a request URL of 
    // "http://whatever.com/path/to/object?id=123"
    // You can pick and choose what you need
    
    var response = client.Execute(request);
    
    MyObject obj = response.Data;
    

    Check out http://restsharp.org/ to get started.

提交回复
热议问题