Get JSON response using RestSharp

后端 未结 4 1502
长发绾君心
长发绾君心 2021-01-08 00:40

I\'m new to C# and I\'m trying to get the JSON response from a REST request using RestSharp; The request I want to execute is the following one : \"http://myurl.com/ap

4条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-08 00:53

    If you want to save the result into JSON file: You should use these namespaces:

    using RestSharp;

    using Newtonsoft.Json;

    using Newtonsoft.Json.Linq;

    var client = new RestClient("http://myurl.com/api/");
    var request = new RestRequest(Method.GET);
    request.AddHeader("content-type", "application/json");
    var queryResult = client.Execute(request).Data;
    string json = JsonConvert.SerializeObject(queryResult);
    System.IO.File.WriteAllText(@"C:\...\path.json", json);
    
        

    提交回复
    热议问题