1

I'm using Json.net and this is my json:

{
    "miner": "t1biSMuXyq5eKkP8zDPViKHF9mbS9ZFLRuF",
    "totalHash": 1523943880.832757,
    "totalShares": 4089.53223605,
    "networkSols": "7644579",
    "immature": 0.29784511,
    "balance": 0,
    "paid": 1092.22974323,
    "workers": {
        "t1biSMuXyq5eKkP8zDPViKHF9mbS9ZFLRuF": {
            "name": "t1biSMuXyq5eKkP8zDPViKHF9mbS9ZFLRuF",
            "diff": 54500,
            "shares": 106.44625040000004,
            "invalidshares": 0,
            "currRoundShares": 4082.8793454,
            "currRoundTime": 0,
            "hashrate": 1523943880.832757,
            "hashrateString": "3.05 KSol/s",
            "luckDays": "5.574",
            "luckHours": "133.768",
            "paid": 1092.22974323,
            "balance": 0
        }
    },
    "history": {
        "t1biSMuXyq5eKkP8zDPViKHF9mbS9ZFLRuF": [
            {
                "time": 1516522247,
                "hashrate": 1600923951.4915187
            },
            {
                "time": 1516526867,
                "hashrate": 1523943880.832757
            }
        ]
    }
}

I would like to get element hashrateString inside "workers" and inside "t1biSMuXyq5eKkP8zDPViKHF9mbS9ZFLRuF" but the "t1biSMuXyq5eKkP8zDPViKHF9mbS9ZFLRuF" changes every time.

This is my current code:

String dl = webClient.DownloadString("http://hushpool.cloud/api/worker_stats?t1biSMuXyq5eKkP8zDPViKHF9mbS9ZFLRuF");
JObject json = JObject.Parse(dl);
JObject workers = JObject.Parse(JObject.Parse(dl).SelectToken("workers").First.ToString());
label7.Text = (String)json.GetValue("balance");
label8.Text = (String)json.GetValue("pending");
label9.Text = (String)json.GetValue("paid");
label10.Text = (String)workers.GetValue("hashrateString");

I've tried different codes but nothing worked. Could you guys help me?

1
  • 2
    I've cut down the json to bare essentials, and formatted it a bit so its easier to understand. If you feel like something is missing, please put it back. Commented Jan 21, 2018 at 10:52

2 Answers 2

1

Since minerValue is not constant, you need to get it from the json in the first place and use it to traverse the JObject to get the value of the hashrateString

JObject obj = JObject.Parse(json);
JObject workers = (JObject)obj.GetValue("workers");
string minerValue = obj.GetValue("miner").ToString();
JObject minerWorker = (JObject)workers.GetValue(minerValue);
string hashRate = minerWorker.GetValue("hashrateString").ToString();

Update: Per your comment, if there is a scenario in which property name will be slightly different, you may use following code to get the object:

JObject workerMiner = (JObject)workers.Properties().FirstOrDefault(p => p.Name.StartsWith(minerValue)).Value;

Keep in mind that this code is not production ready since you need to handle corner/faulty cases (e.g. worker or miner properties do not exist in json)

Sign up to request clarification or add additional context in comments.

3 Comments

The problem is when "miner" value is not the same as value of the first worker. In some cases "miner" value can be: t1biSMuXyq5eKkP8zDPViKHF9mbS9ZFLRuF but first element in "workers" can be: t1biSMuXyq5eKkP8zDPViKHF9mbS9ZFLRuF.worker1
@TomažČuk please look at the updated answer, basically workers object will be traversed and first property which starts with "t1biSMuXyq5eKkP8zDPViKHF9mbS9ZFLRuF" will be returned.
Thank you, this is what I've been looking for!
1

Here is another way to retrieve 'hashratestring' from you json text using jsonpath.

JObject obj = JObject.Parse(jsonTxt);
string name = (string)obj.SelectToken("$.workers.*.hashrateString");
Console.WriteLine(name);

Output:

3.05 KSol/s

Working sample at https://dotnetfiddle.net/wl7fUw

Comments

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.