Reputation: 25
I am attempting to get students from an api and display their information but am having some trouble deserializing some json that I am retrieving. I am using c#
Here is what is being retrieved:
[
{
"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"
}
]
Here is what I have for my 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; }
}
And Finally my code deserializing the json. I cannot provide the actual url here so the below baseUrl is just a placeholder for this 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);
}
}
When this code runs I receive the following error:
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]'.
Thanks in advance for the help.
Upvotes: 0
Views: 63
Reputation: 834
change public string[] Links
for public Link[] Links { get; set; }
use this site for convert your json to c# class
your models 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; }
}
and deserialize
var c = JsonConvert.DeserializeObject<Student[]>(studentResponse);
check example here
Upvotes: 0
Reputation: 43969
there is a bug in json, add " to lastName before : . Also fix Links property and add GPA property
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; }
}
Upvotes: 0
Reputation: 150
It seems like your Links
property of class Student
should be type of Links
instead of string[]
.
I would also suggest renaming the Links
class to Link
Upvotes: 1