我希望有人能帮我解决这个问题;我刚刚开始在一个需要进行API调用的网站上工作。我使用开源库进行 API 调用。大多数电话都很好用,但我无法让最重要的电话工作。反序列化时的 json 字符串返回一个空对象。
JSON 字符串:
{"benbeun":{"id":27266833,"name":"BenBeun","profileIconId":25,"summonerLevel":30,"revisionDate":1393655593000}}
调用,其中响应文本是上面的 JSON 字符串:
public static T CreateRequest(string url)
{
var result = new T();
var getRequest = (HttpWebRequest)WebRequest.Create(url);
using (var getResponse = getRequest.GetResponse())
using (var reader = new System.IO.StreamReader(getResponse.GetResponseStream()))
{
var responseText = reader.ReadToEnd();
result = JsonConvert.DeserializeObject<T>(responseText);
}
return result;
}
库中的默认类:
public class SummonerDto
{
/// <summary>
/// Summoner ID.
/// </summary>
[JsonProperty("id")]
public long Id { get; set; }
/// <summary>
/// Summoner name.
/// </summary>
[JsonProperty("name")]
public string Name { get; set; }
/// <summary>
/// ID of the summoner icon associated with the summoner.
/// </summary>
[JsonProperty("profileIconId")]
public int ProfileIconId { get; set; }
/// <summary>
/// Date summoner was last modified specified as epoch milliseconds.
/// </summary>
[JsonProperty("revisionDate")]
public long RevisionDate { get; set; }
/// <summary>
/// Summoner level associated with the summoner.
/// </summary>
[JsonProperty("summonerLevel")]
public long SummonerLevel { get; set; }
}
我可以让下面的类在我的调用中工作;但是'benbeun'字符串是可变的,所以这个类不能使用。
public class Benbeun
{
public int id { get; set; }
public string name { get; set; }
public int profileIconId { get; set; }
public int summonerLevel { get; set; }
public long revisionDate { get; set; }
}
public class SummonerDto
{
public Benbeun benbeun { get; set; }
}
有什么指示吗?我已经尝试了其他问题中提供的众多选项,但我担心我的知识缺乏我的问题到底在哪里。我觉得我与下面的代码很接近,但它也返回了一个空对象。
public class SummonerDto
{
public IDictionary<string, Summoner> Summoner { get; set; }
}
public class Summoner
{
/// <summary>
/// Summoner ID.
/// </summary>
[JsonProperty("id")]
public long Id { get; set; }
/// <summary>
/// Summoner name.
/// </summary>
[JsonProperty("name")]
public string Name { get; set; }
/// <summary>
/// ID of the summoner icon associated with the summoner.
/// </summary>
[JsonProperty("profileIconId")]
public int ProfileIconId { get; set; }
/// <summary>
/// Date summoner was last modified specified as epoch milliseconds.
/// </summary>
[JsonProperty("revisionDate")]
public long RevisionDate { get; set; }
/// <summary>
/// Summoner level associated with the summoner.
/// </summary>
[JsonProperty("summonerLevel")]
public long SummonerLevel { get; set; }
}
使用 Dictionary<string,Summoner>
进行反序列化。
var obj = JsonConvert.DeserializeObject<Dictionary<string,Summoner>>(json);