1

How can i grab all tokens in a List<String> ?
Here is the json string :

{
    "error": null,
    "jsonrpc": "2.0",
    "id": 0,
    "result": {
        "paginator": null,
        "cat_count": {},
        "last_post_date": 386623075949839,
        "post_list": [
            {
                "hc": false,
                "p2": 4,
                "token": "LnARJZCmt"
            },
            {
                "hc": false,
                "p2": 4,
                "token": "BuD2oIs3N"
            },
            {
                "p2": 4,
                "token": "89NaBsAha",
                "hc": false,
            }
        ],
        "error": 0
    }
}

I am using Newtonsoft.Json
Here is what i tried :

    var obj = JObject.Parse(json);
    string next_search_val = (string)obj["result"]["last_post_date"];
    var tokens = obj["result"]["post_list"]["token"]; > Fix this line for me > I have error here

1 Answer 1

5

I would use

var tokens = JObject.Parse(json)
                .SelectTokens("$..token")
                .Select(x => (string)x)
                .ToList();

EDIT

Same thing without JsonPath

var tokens = JObject.Parse(json)
                .Descendants()
                .OfType<JProperty>()
                .Where(x => x.Name == "token")
                .Select(x => x.Value)
                .ToList();

EDIT 2

Closest thing to your attempt

var tokens = JObject.Parse(json)["result"]["post_list"]
                .Select(x => (string)x["token"])
                .ToList();
Sign up to request clarification or add additional context in comments.

3 Comments

$.. > What is that mean?
Appreciate it & Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.