我使用以下代码从 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
data 作为字符串,然后检查它是否包含background
, emoticon
or trading card
.
我知道我可以使用if(type.Contains("background"))
检查项目类型是什么,但我在 foreach 循环中遇到问题。
如果我使用foreach(JObject x in ja)
我得到一个cannot convert type
Error.
如果我使用foreach(Object x in ja)
它提出了一个Cannot apply indexing of type object
.
当我使用时也会出现此错误foreach(var x in ja)
and string type = (JObject)x["type"];
谁能告诉我我做错了什么吗?