问题
I need to write to List<string>
the list of universities. I already had written countries and cities to lists. But in universities data is represented a little bit different. There is {"response":[201,
and I do not have any idea how to handle with it. I successfully got countries from here. And from here I need to write universities titles to a List<string>
. This is code how did I got countries. The analogical code I used in cities. While trying to execute this line var universityRepository = new RootObject((int)universityInResponse["id"], (string)universityInResponse["title"]);
I got an unhandled exception. The full code of the class:
public class GettingUniversity
{
public static List<RootObject> listOfUniversitiesRoot = new List<RootObject>();//This List contains Id and Titles of universities
public List<string> listOfUniversities = new List<string>();//list with names of the universities
private string jsonString; //string for getting data from the url
public async Task<List<RootObject>> FetchAsync(string url)
{
//getting data process goes here
using (var httpClient = new System.Net.Http.HttpClient())
{
var stream = await httpClient.GetStreamAsync(url);
StreamReader reader = new StreamReader(stream);
jsonString = reader.ReadToEnd();
}
var responseUniversities = JArray.Parse(JObject.Parse(jsonString)["response"].ToString());//parsing data from jsonstring
foreach (var universityInResponse in responseUniversities)//the foreach-loop
{
var universityRepository = new RootObject((int)universityInResponse["id"], (string)universityInResponse["title"]);
//listOfUniversitiesRoot.Add(universityRepository);//adding to the list with names of the universities
//listOfUniversities.Add(universityRepository.Title);
}
return listOfUniversitiesRoot;//returned list
}
Properties are represented here:
namespace KoshelnykTestTask
{
//Here I have set properties for FetchAsync(string url) to get countries and cities
public class RootObject
{
public int Id { get; set; }
public string Title { get; set; }
public RootObject(int Id, string Title)
{
this.Id = Id;
this.Title = Title;
}
}
}
回答1:
Your problem is that the JSON returned contains an array that is polymorphic -- the first entry is an integer, and the remainder are objects containing university information:
{
"response":[
201,
{
"id":1096,
"title":"КПИ им. И. Сикорского"
},
{
"id":1135,
"title":"НУФВСУ (бывш. КГИФК)"
},
{
"id":1140,
"title":"КГАВТ им. Конашевича-Сагайдачного"
},
You need to filter the integer value before deserializing the objects, which can be done as follows:
var universityRepository = JToken.Parse(jsonString)["response"]
// Filter the integer value by selecting only objects
.OfType<JObject>()
// Deserialize each object to a RootObject
.Select(o => o.ToObject<RootObject>())
// Return in a List<RootObject>
.ToList();
var listOfUniversities = universityRepository
.Select(u => u.Title)
.ToList();
Sample fiddle.
Note there seems to be no need to store these lists in static or class variables -- you can simply return them to the caller.
来源:https://stackoverflow.com/questions/42939991/parse-universities-from-vk-api