c# - 遍历多个 JObject 级别并将信息收集为字符串

标签 c# json foreach

我正在使用以下代码从 URL 收集 Json 数据。

            var json = new WebClient().DownloadString("http://steamcommunity.com/id/tryhardhusky/inventory/json/753/6");
            JObject jo = JObject.Parse(json);
            JObject ja = (JObject)jo["rgDescriptions"];

            int cCount = 0;
            int bCount = 0;
            int eCount = 0;

            foreach(var x in ja){

                // I'm stuck here.
                string type = (Object)x["type"];

            }

            CUAI.sendMessage("I found: " + ja.Count.ToString());

在我到达 foreach 语句之前,一切都运行良好。
这是 JSON 数据的片段。

{
    "success": true,
    "rgInventory": {
        "Blah other stuff"
    },
    "rgDescriptions": {
        "637390365_0": {
            "appid": "753",
            "background_color": "",
            "type": "0RBITALIS Trading Card"
        "175240190_0": {
            "appid": "753",
            "background_color": "",
            "type": "Awesomenauts Trading Card"
        },
        "195930139_0": {
            "appid": "753",
            "background_color": "",
            "type": "CONSORTIUM Emoticon"
        }
    }
}

我想遍历 rgDescriptions 中的每个项目并获取字符串形式的 type 数据,然后检查它是否包含 backgroundemoticon交易卡
我知道我可以使用 if(type.Contains("background")) 来检查项目类型是什么,但是我在使用 foreach 循环时遇到了问题。

如果我使用 foreach(JObject x in ja) 我会得到一个 cannot convert type 错误。
如果我使用 foreach(Object x in ja) 它会出现一个 Cannot apply indexing of type object
当我使用 foreach(var x in ja)string type = (JObject)x["type"];

时也会发生此错误

谁能告诉我我做错了什么,拜托?

最佳答案

您的 JSON 中有一些错误。使用 jsonlint.com 检查它。我认为它应该看起来像这样:

{
    "success": true,
    "rgInventory": {
        "Blah other stuff": ""
    },
    "rgDescriptions": {
        "637390365_0": {
            "appid": "753",
            "background_color": "",
            "type": "0RBITALIS Trading Card"
        },
        "175240190_0": {
            "appid": "753",
            "background_color": "",
            "type": "Awesomenauts Trading Card"
        },
        "195930139_0": {
            "appid": "753",
            "background_color": "",
            "type": "CONSORTIUM Emoticon"
        }
    }
}

您可以使用 JProperty、JToken 和 SelectToken 方法来获取类型:

var json = new WebClient().DownloadString("http://steamcommunity.com/id/tryhardhusky/inventory/json/753/6");
JObject jo = JObject.Parse(json);

foreach (JProperty x in jo.SelectToken("rgDescriptions"))
{
    JToken type = x.Value.SelectToken("type");
    string typeStr = type.ToString().ToLower();

    if (typeStr.Contains("background"))
    {
        Console.WriteLine("Contains 'background'");
    }
    if (typeStr.Contains("emoticon"))
    {
        Console.WriteLine("Contains 'emoticon'");
    }
    if (typeStr.Contains("trading card"))
    {
        Console.WriteLine("Contains 'trading card'");
    }
}

关于c# - 遍历多个 JObject 级别并将信息收集为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29741893/

相关文章:

javascript - 循环遍历 JSON 并动态填充数据

c# - 在代码隐藏中将数据网格列宽设置为星号

c# - Assert.AreEqual<T>(和相关的通用函数)不强制执行它们的参数类型

c# - .net Reflection.Emit 简化框架?

python - 如何在 Python 中使用 JSONDecoder?只获取内部字典进行解码

javascript - js - 如何将表数据转换为树 (JSON)

PHP:变量的双引号内的 if 或 foreach 语句

c# - Foreach 问题

c# - 在 Visual Studio 2019 中升级 xproj

javascript - 如何使用 Regex 将两个双引号替换为一个双引号而不捕获一个双引号的出现?