反序列化问题 JSON

Trouble Deserializing JSON

我试图从 api 中获取学生并显示他们的信息,但在反序列化我正在检索的某些 json 时遇到了一些问题。我正在使用 c#

这是正在检索的内容:

    [
       { 
          "firstName": "John", 
          "lastName: "Smith", 
          "id": "122386144", 
          "schoolYear": "Sophmore", 
          "Links": [
              { 
                 "url": "https://github.com/johnsmith", 
                 "comments": "Click to see some of my projects!"
              }
          ],
          "GPA": "3.6"
        },
        { 
          "firstName": "Jane", 
          "lastName: "Doe", 
          "id": "45624523", 
          "schoolYear": "Junior", 
          "Links": [
              { 
                 "url": "https://linkedin.com/janedoe", 
                 "comments": "Follow me on LinkedIn"
              }
          ],
          "GPA": "3.8"
        }
    ]

这是我的 class:

public class Student
{
   public string firstName { get; set; }
   public string lastName { get; set; }
   public int id { get; set; }
   public string schoolYear { get; set; }
   public string[] Links { get; set; }
}

public class Links
{
   public string url { get; set; }
   public string comments { get; set; }
}

最后我的代码反序列化 json。我不能在这里提供实际的 url 所以下面的 baseUrl 只是这个 post:

的占位符
string baseUrl = "https://CodeUniversity.com/"
List<Student> studentList = new List<Student>();

using (var client = new HttpClient())
{
   client.BaseAddress = new Uri(baseUrl);
   client.DefaultRequestHeaders.Clear();
   client.DeafultRequestHeaders.Accept.Add(new 
      MediaTypeWithQualityHeaderValue("application/json"));
   HttpResponseMessage response = await client.GetAsync("api/CS/students");
   
   if (response.IsSuccessStatusCode)
   {
      var studentResponse = response.Content.ReadAsStringAsync().Result;
      studentList = JsonConvert.DeserializeObject<List<Student>(studentResponse);
   }
}

运行此代码时,我收到以下错误:

The model item passed into the dictionary is of type 'Newtonsoft.Json.JsonReaderException', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[University.Models.Student]'.

在此先感谢您的帮助。

class StudentLinks 属性 似乎应该是 Links 而不是 string[] 的类型。 我还建议将 Links class 重命名为 Link

json 中存在错误,在 : 之前的 lastName 添加 "。同时修复链接 属性 并添加 GPA 属性

public class Student
{
    public string firstName { get; set; }
    public string lastName { get; set; }
    public  int id { get; set; }
    public string schoolYear { get; set; }
    public double GPA { get; set; }
    public List<Link> Links { get; set; }
}

public class Link
{
   public string url { get; set; }
   public string comments { get; set; }
}

public string[] Links 更改为 public Link[] Links { get; set; }

使用此网站转换您的 json to c# class

你的模型class

public partial class Student
    {
        [JsonProperty("firstName")]
        public string FirstName { get; set; }

        [JsonProperty("lastName")]
        public string LastName { get; set; }

        [JsonProperty("id")]
        public string Id { get; set; }

        [JsonProperty("schoolYear")]
        public string SchoolYear { get; set; }

        [JsonProperty("Links")]
        public Link[] Links { get; set; }

        [JsonProperty("GPA")]
        public string Gpa { get; set; }
    }

    public partial class Link
    {
        [JsonProperty("url")]
        public Uri Url { get; set; }

        [JsonProperty("comments")]
        public string Comments { get; set; }
    }

并反序列化

var c = JsonConvert.DeserializeObject<Student[]>(studentResponse);

检查例子here