遍历多个JObject级别并以字符串形式收集信息 [英] Looping through multiple JObject levels and gathering information as a string
问题描述
我正在使用以下代码从URL收集Json数据.
I'm using the following code to gather Json data from a URL.
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数据的一个片段.
Everything is working well until I get to the foreach statement.
Here is a snippet of the JSON Data.
{
"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
数据,然后检查它是否包含background
,emoticon
或trading card
.
我知道我可以使用if(type.Contains("background"))
来检查项目类型,但是我在foreach循环中遇到了麻烦.
I'm wanting to loop through each item in rgDescriptions and get the type
data as a string, Then check if it contains either background
, emoticon
or trading card
.
I know I can use the if(type.Contains("background"))
to check what the item type is, But I'm having trouble with the foreach loop.
如果使用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"];
If I use foreach(JObject x in ja)
I get a cannot convert type
Error.
If I use foreach(Object x in ja)
It comes up with a Cannot apply indexing of type object
.
This error also happens when I use foreach(var x in ja)
and string type = (JObject)x["type"];
有人可以告诉我我在做什么错吗?
Can anyone tell me what I'm doing wrong, Please?
推荐答案
您的JSON中有一些错误.使用jsonlint.com进行检查.我认为应该看起来像这样:
You have some errors in your JSON. Check it with jsonlint.com. I think it should look something like this:
{
"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方法获取类型:
You can use the JProperty, JToken and the SelectToken Method to get the type:
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'");
}
}
这篇关于遍历多个JObject级别并以字符串形式收集信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!