There is a function.

public void Func() { var publicTL = new WebClient().DownloadString(@"link"); var statuses = DynamicJson.Parse(publicTL); Label1.Text = statuses.data.hashRate.ToString(); } 

I want to remake it, so that it could be called something like this Label1.Text = Func(data.hashRate) , i.e. I want to pass only method parameters. I don’t even understand what type the function will take, will return a string. How can this be implemented? PS DynamicJson library for json parsing.

  • public var ? is that right? - Zufir
  • I am not good at creating functions, therefore I cannot vouch for the correctness. If you know, correct, I will be glad - Pavel Fedorov

1 answer 1

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(); } 
  • I’m probably a little wrong in describing the essence of the question, I want to pass different arguments to the function, i.e. data.hashRate this part in the returned string will change. Because This parser library and parsit is Json, and parsing is not one value. If it is possible to do so? I hope you understand what I - Pavel Fedorov
  • Not really. Do you need a method that takes arbitrary field from arbitrary JSON? Give an example of the data and what you want to receive with different calls to your function. - Zufir
  • Here is an example of bin.codingislove.com/giruderiwo.json , for example, I want to get the values ​​of statuses.data.hashRate and statuses.data.block , etc. (there may be a lot of values), but I don’t want to describe specific values ​​in the function, but I want to call the function as necessary to retrieve specific values, in this case hashRate and block. Those. That library DynamicJson would accept the arguments received through function. Maybe I, that is what I call not by their proper names. my theory is lame. - Pavel Fedorov
  • @PavelFedorov, well, then return from the f-and DynamicJson , and select the required fields on the client side - Andrey NOP
  • @Zufir Thank you, now I understand the specifics, judging by the code, it was not difficult :) - Pavel Fedorov