使用变量名称反序列化为对象

时间:2014-03-05 09:10:25

标签: c# json.net deserialization

我希望有人可以帮助我;我刚刚开始在一个需要进行API调用的网站上工作。我使用开源库进行API调用。大多数电话都很棒,但我无法找到最重要的电话。反序列化时的json字符串返回一个空对象。

JSON字符串:

{"benbeun":{"id":27266833,"name":"BenBeun","profileIconId":25,"summonerLevel":30,"revisionDate":1393655593000}}

调用,其中responseText是上面的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; }

}

1 个答案:

答案 0 :(得分:0)

使用Dictionary<string,Summoner>进行反序列化。

var obj = JsonConvert.DeserializeObject<Dictionary<string,Summoner>>(json);