Deserialize array of key value pairs using Json.NET

前端 未结 4 1357
南笙
南笙 2020-12-03 13:51

Given the following json:

[ {\"id\":\"123\", ... \"data\":[{\"key1\":\"val1\"}, {\"key2\":\"val2\"}], ...}, ... ]

that is part of a bigger

4条回答
  •  感动是毒
    2020-12-03 14:17

    public class Datum
    {
        public string key1 { get; set; }
        public string key2 { get; set; }
    }
    
    public class RootObject
    {
        public string id { get; set; }
        public List data { get; set; }
    }
    

    i used this wizard as well json2csharp.com to generate class for deserialized

    for using that

    using RestSharp;
    using Newtonsoft.Json;
    
    IRestResponse restSharp= callRestGetMethodby_restSharp(api_server_url);
    string jsonString= restSharp.Content;
    
    RootObject rootObj= JsonConvert.DeserializeObject(jsonString);
    return Json(rootObj);
    

    if you call rest by restsharp

        public IRestResponse callRestGetMethodby_restSharp(string API_URL)
        {
            var client = new RestSharp.RestClient(API_URL);
            var request = new RestRequest(Method.GET);
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("cache-control", "no-cache");
            IRestResponse response = client.Execute(request);
            return response;
        }
    

    also you can get this 6 line of restsharp from getpostman.com tools

提交回复
热议问题