【问题标题】:JSON.NET: check first key value in Json fileJSON.NET:检查 Json 文件中的第一个键值
【发布时间】:2014-01-13 10:35:58
【问题描述】:

我想检查 JSON 中第一个标签的值。我做了以下:

JObject o = JObject.Parse(@response);        
switch (o.First.ToString())
{
    case "players":
    {
        //do something
    }
    case "errors":
    {
        //do something
    }
}

当我尝试打印 o.first.tostring() 时,我得到了整个 JSON 字符串,而不是第一个标签。这里有什么问题? 编辑:我不知道收到的 JSON 是 :{Players:{"id":"xyz"}} 还是 {errors:{"id":"123"}}

【问题讨论】:

    标签: c# asp.net .net json json.net


    【解决方案1】:

    在您的情况下(使用提供的 JSON 格式),它将类似于:

    JObject o = JObject.Parse(response);
    
    switch (o.First.First.Path)
    {
        case "Players":
            // do stuff 
            break;
        // ...
    }
    

    【讨论】:

    • 路径是什么?也没有ToString()?
    • 工作 :) 非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2018-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-19
    • 2022-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多