如何将 json 转换为 C# 中的模型列表?

how to cast json to list of models in c#?

我想调用一个api,这个api的答案是Json形式的输出列表。如何设置模型列表? 我的模特

public class JsonModel
    {
        public bool IsAuthorize{ get; set; }
        public string Service { get; set; }
        public int Age{ get; set; }
    }

使用 Newtonsoft,这是最流行的 JSON .NET 框架:

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RestSharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

static void Main(string[] args)
    {
        var client = new RestClient("your api url");
        client.Timeout = -1;
        var request = new RestRequest("Method type");
        var response = client.Execute(request);
        var res = JsonConvert.DeserializeObject<JObject>(response.Content);

        foreach (var token in res.Children())
        {
            if (token is JProperty)
            {
                var prop = JsonConvert.DeserializeObject<JsonModel>(token.First().ToString());
                Console.WriteLine($"IsAuthorize : {prop.IsAuthorize} , Service : {prop.Service} , Age : {prop.Age} ");
            }
        }
        Console.ReadKey();
    }

    public class JsonModel
    {
        public bool IsAuthorize { get; set; }
        public string Service { get; set; }
        public int Age { get; set; }
    }