我正在搞乱Steam Web API。我正在尝试创建一个函数,它将找到一段文本,然后提取另一段文本,这将改变,这取决于我用函数查找的用户。
我尝试过使用IndexOf,但由于我想提取的文字长度各不相同,所以很难。
Json响应我想从这样的字符串中提取字符串,但会因我查找的每个用户而异。
{
"response": {
"game_count": 54,
"games": [
{
"appid": 240,
"playtime_forever": 51842
},
{
"appid": 260,
"playtime_forever": 43
},
{
"appid": 300,
"playtime_forever": 191
},
{
"appid": 320,
"playtime_forever": 130
},
{
"appid": 340,
"playtime_forever": 4
},
{
"appid": 520
},
{
"appid": 4000,
"playtime_2weeks": 798,
"playtime_forever": 61803
},
{
"appid": 440,
"playtime_forever": 19466
},
{
"appid": 105600,
"playtime_forever": 2084
},
{
"appid": 72850,
"playtime_forever": 2010
},
{
"appid": 380,
"playtime_forever": 91
},
{
"appid": 400,
"playtime_forever": 76
},
{
"appid": 420
},
{
"appid": 220,
"playtime_forever": 827
},
{
"appid": 550,
"playtime_forever": 906
},
{
"appid": 33910,
"playtime_forever": 77
},
{
"appid": 33930,
"playtime_2weeks": 1714,
"playtime_forever": 31904
},
{
"appid": 219540,
"playtime_forever": 0
},
{
"appid": 570
},
{
"appid": 205790
},
{
"appid": 22380,
"playtime_forever": 1119
},
{
"appid": 105400,
"playtime_forever": 134
},
{
"appid": 204030
},
{
"appid": 12120
},
{
"appid": 12250
},
{
"appid": 24010
},
{
"appid": 8190,
"playtime_forever": 723
},
{
"appid": 620,
"playtime_forever": 101
},
{
"appid": 644
},
{
"appid": 730,
"playtime_forever": 431
},
{
"appid": 10,
"playtime_forever": 396
},
{
"appid": 80
},
{
"appid": 100
},
{
"appid": 42680,
"playtime_forever": 65
},
{
"appid": 42690,
"playtime_forever": 1435
},
{
"appid": 22200
},
{
"appid": 113200,
"playtime_forever": 270
},
{
"appid": 20540
},
{
"appid": 55110
},
{
"appid": 43110
},
{
"appid": 9340
},
{
"appid": 50620
},
{
"appid": 4560
},
{
"appid": 55230,
"playtime_forever": 1354
},
{
"appid": 4540
},
{
"appid": 4570
},
{
"appid": 22370,
"playtime_forever": 305
},
{
"appid": 70,
"playtime_forever": 479
},
{
"appid": 70300,
"playtime_forever": 138
},
{
"appid": 220240,
"playtime_2weeks": 72,
"playtime_forever": 453
},
{
"appid": 107410,
"playtime_2weeks": 80,
"playtime_forever": 403
},
{
"appid": 40800,
"playtime_2weeks": 124,
"playtime_forever": 124
},
{
"appid": 12210,
"playtime_2weeks": 663,
"playtime_forever": 663
},
{
"appid": 12220,
"playtime_2weeks": 658,
"playtime_forever": 658
}
]
}
}
我想要做的是搜索“\”appid \“:440”,然后能够得到之后的playtime_forever int。我无能为力。如果字符串有一个设置的长度,我只能从Split函数中获取帮助,因为那时我可以返回我想要的数组索引。
如果不清楚的话,我非常抱歉,我尽力解释我想要实现的目标。
提前致谢,
伊萨克
答案 0 :(得分:0)
正如评论所说,你所看到的不是一个简单的字符串,所以不要尝试为它编写自己的解析器。这是一个包含对象集合的JSON响应。这些对象似乎有3个字段:
如果您希望在代码中本地使用这些内容,只需创建一个具有三个公共属性(getter& setter)的传输对象,其中每个属性与JSON字段具有相同的名称。然后只需使用System.WEb.Script
命名空间的JavaScriptSerializer将集合反序列化为List<TransportObject>
。
答案 1 :(得分:0)
你可以尝试将Json反序列化为动态对象,看看这里: Deserialize JSON into C# dynamic object?
然后遍历您的应用集合并检查appId = 440
for(int i = 0; i <= deserializedResult.Count; i++)
{
if(deserializedResult[i] == 440)
....
}
似乎playtime_forever并不总是可用,所以我猜你必须使用反射来看看playtime_forever是否存在才能访问它。