1

I'm messing around with the Steam Web API. I'm trying to make a function which will find a piece of text, and then extract another piece of text, which will change, depending on the user I look up with a function.

I've tried using IndexOf, but since the text I'd like to extract can vary in length, it's rather hard.

The Json response I want to extract the string from looks like this, but will vary from each user I look up.

{
"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
        }
    ]

}

}

What I want to do, is to search for i.e. "\"appid\": 440", and then be able to get the playtime_forever int which comes after. I'm clueless as what to do. I would only be able to take help from the Split function if the string had a set length, because then I could return the array index I wanted to.

I'm terribly sorry if this came out unclear, I tried my best explaining what I'm trying to achieve.

Thanks in advance,

Isak

Isak
  • 365
  • 3
  • 11

2 Answers2

0

As the comments have said, what you're looking at isn't a simple string, so don't try writing your own parser for it. This is a JSON response containing a collection of objects. These objects seem to have 3 fields:

  • appId
  • playtime_forever
  • playtime_2Weeks

If you want to work with these natively in your code simply create a transport object with three public properties (getter & setter) where each property has the same name as the JSON field. Then simply use the System.WEb.Script namespace's JavaScriptSerializer to deserialize the collection into a List<TransportObject>.

Chris
  • 2,885
  • 18
  • 25
0

You could try deserialize the Json to a dynamic object, take a look here: Deserialize JSON into C# dynamic object?

and then loop through your collection of apps and check for appId = 440

for(int i = 0; i <= deserializedResult.Count; i++)
{
    if(deserializedResult[i] == 440)
        ....
}

It seems like playtime_forever isn't always available so i guess you'll have to use reflection to see wether playtime_forever exists or not before accessing it.

Community
  • 1
  • 1
jonnep
  • 285
  • 3
  • 17