简体   繁体   中英

Best efficient way of converting JSON string to model object

I have various HttpClient calls which return back a json string. I am currently using the following code to convert the string to a custom 'object' model I have:

 public static async Task<List<Models.GSAModels.Meeting>> GetMeetingsAndRacesOnDate(DateTime date)
    {
        string res = "";

        using (HttpClient client = new HttpClient())
        {
            client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");

            using (HttpResponseMessage response = await client.GetAsync("url" + date.ToString("yyyy-MM-dd") + "?gsaappkey=9999"))
            {
                res = await response.Content.ReadAsStringAsync();
            }
        }

        JObject jobject = JObject.Parse(res);
        List<Models.GSAModels.Meeting> list = jobject["value"].ToObject<List<Models.GSAModels.Meeting>>();

        return list;
    }

Is this the most effecient way of doing so?

Thank you

You can use Newtownsoft.Json nuget package

using Newtonsoft.Json;
...
var result = JsonConvert.DeserializeObject<T>(json);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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