Я сделал код для разбора этого JSONJSON Десериализация Исключение
Я генерируемый класс с JSON2C#:
public class Link
{
public string self { get; set; }
public string soccerseason { get; set; }
}
public class Self
{
public string href { get; set; }
}
public class Fixtures
{
public string href { get; set; }
}
public class Players
{
public string href { get; set; }
}
public class Links
{
public Self self { get; set; }
public Fixtures fixtures { get; set; }
public Players players { get; set; }
}
public class Team
{
public Links _links { get; set; }
public string name { get; set; }
public string code { get; set; }
public string shortName { get; set; }
public string squadMarketValue { get; set; }
public string crestUrl { get; set; }
}
public class RootObject
{
public List<Link> _links { get; set; }
public int count { get; set; }
public List<Team> teams { get; set; }
}
Я выполнить запрос по этому пути:
private void League_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string requestUrl = "http://api.football-data.org/alpha/soccerseasons/122/teams";
HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
request.Method = "GET";
request.ContentType = "application/json";
string responseText;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
using (var responseStream = new StreamReader(response.GetResponseStream()))
{
responseText = responseStream.ReadToEnd();
}
List<RootObject> obj = JsonConvert.DeserializeObject<List<RootObject>>(responseText);
foreach (var item in obj)
{
Home.Items.Add(item.name);
}
}
компилятор вернет меня:
Unhandled Exception "Newtonsoft.Json.JsonSerializationException" in Newtonsoft.Json.dll Cannot deserialize the current JSON object (e.g {"name":"value"}) into type "System.Collections.Generic.List"1 [Test.MainWindow+RootObject]" because the type requires a Json array (e.g. [1,2,3]) to deserialize correctly.
на obj десериализация.
Как это исправить? Как я могу выбрать правильную десериализацию?
Try http://www.httputility.net/json-to-csharp-vb-typescript-class.aspx – Nissim