You need to get some value, parse it and return one of its fields. To do this, we describe the method GetHashRate, which will take as input a string with an address and return a string with a hashrate.
UPD. We want to return not only HashRate, but also any other property. To do this, add another propertyName parameter. In it we will pass the required property, the benefit of the library used allows the use of indexers:
public static string GetData(string link, string propertyName) { var publicTL = GetJson(link); var statuses = DynamicJson.Parse(publicTL); return statuses["data"][propertyName].ToString(); } private static string GetJson(string link) { return "{\"status\":1,\"data\":{\"hashRate\":\"119.9 MH/s\",\"block\":166479}}"; }
Here I made the data acquisition in a separate method (the data from your link is not a valid JSON object, since this is an HTML page). If you will use - do not forget to return the receipt of data on the link.
Then we can use the result of that method:
Label1.Text = GetData(link, "hashRate");
Or to check in the console:
static void Main(string[] args) { Console.WriteLine(GetHashRate(@"http://bin.codingislove.com/giruderiwo.json", "hashRate")); Console.WriteLine(GetHashRate(@"http://bin.codingislove.com/giruderiwo.json", "block")); Console.ReadKey(); }
public var? is that right? - Zufir