C#JSON返回列表内部列表

时间:2016-04-26 13:58:02

标签: c# json list

public class Entry
{
    public string playerOrTeamId { get; set; }
    public string playerOrTeamName { get; set; }
    public string division { get; set; }
    public int leaguePoints { get; set; }
    public int wins { get; set; }
    public int losses { get; set; }
    public bool isHotStreak { get; set; }
    public bool isVeteran { get; set; }
    public bool isFreshBlood { get; set; }
    public bool isInactive { get; set; }
}

public class SummonerId
{
    public string name { get; set; }
    public string tier { get; set; }
    public string queue { get; set; }
    public List<Entry> entries { get; set; }
}

public class RootObject
{
    public List<SummonerId> Summoner_Id { get; set; }
}

我使用 Json2csharp.com 创造了这个课程。

如果班级有1个列表,我可以毫无问题地访问数据。

但是这个类生成了2个列表。我想我现在已经过度思考并且变得非常困惑......

如何反序列化此类

string url = json.ToString();

var root = JsonConvert.DeserializeObject<RootObject>(url):

Summoner_Id返回null

var id = root.Summoner_Id;

root也返回null ..

我该如何解决这个问题?请帮助或指出我正确的方向!

1 个答案:

答案 0 :(得分:1)

此示例适用于我:

template<typename Params, typename Descriptor>
struct IsParamsEqual;

template<typename Params1, typename Params2, ApiCommand::Value value>
struct IsParamsEqual<Params1, Descriptor<value, Params2>>
{
    static constexpr bool v = std::is_same<Params1, Params2>::value;
};

template<typename Params, typename IS, typename... Args>
struct GetIndexByParam;

template<typename Param, typename... Args>
struct GetIndexByParam<Param, IndexSequence<>, Args...>
{
    typedef typename std::integral_constant<size_t, 0> type;
};

template <typename Param, size_t I, size_t... IndexesTail,  typename Head, 
    typename ...Tail>
struct GetIndexByParam<Param, IndexSequence<I, IndexesTail...>, Head, Tail...>
{
    typedef typename std::conditional<IsParamsEqual<Param, Head>::v,
        std::integral_constant<size_t, I>,
        typename GetIndexByParam<Param, IndexSequence<IndexesTail...>, Tail...>::type >::type type;
};

template<typename Params, typename... Args>
constexpr auto getByParamsImpl(const std::tuple<Args...> &tuple)
{
    typedef typename GetIndexByParam<Params,        
        typename MakeIndexSequence<sizeof...(Args)>::type, Args...>::type IndexValueType;
    static_assert(std::is_same<
        typename std::remove_reference<decltype(std::get<IndexValueType::value>(tuple))>::type::paramType, \
            Params>::value, "Parameter not found");
    return std::get<IndexValueType::value>(tuple);
}