-2

I need to modify a software. I need to extract the "price" data from this, how can i do?

{
  "result": {
    "stats": [
      {
        "profitability_above_ltc": "0",
        "price": "1.8266",
        "profitability_ltc": "0",
        "algo": 0,
        "speed": "14266.85620849"
      },
      {
        "price": "0.081",
        "profitability_btc": "0",
        "profitability_above_btc": "0",
        "algo": 1,
        "speed": "385908261.68241924"
      },
      {
        "profitability_above_ltc": "0",
        "price": "0",
        "profitability_ltc": "0",
        "algo": 2,
        "speed": "0"
      },
      {
        "profitability_above_ltc": "0",
        "price": "0.0298",
        "profitability_ltc": "0",
        "algo": 3,
        "speed": "595530.02421524"
      },
      {
        "profitability_above_ltc": "0",
        "price": "0.0003",
        "profitability_ltc": "0",
        "algo": 4,
        "speed": "2611.90080988"
      },
      {
        "profitability_above_ltc": "0",
        "price": "0.1404",
        "profitability_ltc": "0",
        "algo": 5,
        "speed": "2260.49880031"
      },
      {
        "profitability_above_ltc": "0",
        "price": "0.0003",
        "profitability_ltc": "0",
        "algo": 6,
        "speed": "1050.4786522"
      },
      {
        "profitability_above_ltc": "0",
        "price": "0.0031",
        "profitability_ltc": "0",
        "algo": 7,
        "speed": "2467.60173312"
      },
      {
        "profitability_above_ltc": "0",
        "price": "0.1354",
        "profitability_ltc": "0",
        "algo": 8,
        "speed": "42.42055349"
      },
      {
        "profitability_above_ltc": "0",
        "price": "0.0001",
        "profitability_ltc": "0",
        "algo": 9,
        "speed": "0.05507848"
      },
      {
        "profitability_above_ltc": "0",
        "price": "0",
        "profitability_ltc": "0",
        "algo": 10,
        "speed": "0"
      },
      {
        "profitability_above_ltc": "0",
        "price": "0.1721",
        "profitability_ltc": "0",
        "algo": 11,
        "speed": "1676.32988734"
      },
      {
        "profitability_above_ltc": "0",
        "price": "0.209",
        "profitability_ltc": "0",
        "algo": 12,
        "speed": "29233.19588795"
      },
      {
        "profitability_above_ltc": "0",
        "price": "0",
        "profitability_ltc": "0",
        "algo": 13,
        "speed": "0"
      },
      {
        "profitability_above_ltc": "0",
        "price": "3.72",
        "profitability_ltc": "0",
        "algo": 14,
        "speed": "4429.92289544"
      },
      {
        "profitability_above_ltc": "0",
        "price": "0",
        "profitability_ltc": "0",
        "algo": 15,
        "speed": "0"
      },
      {
        "profitability_above_ltc": "0",
        "price": "0.0057",
        "profitability_ltc": "0",
        "algo": 16,
        "speed": "1374.37104"
      },
      {
        "profitability_above_ltc": "0",
        "price": "0",
        "profitability_ltc": "0",
        "algo": 17,
        "speed": "0"
      },
      {
        "profitability_above_ltc": "0",
        "price": "0",
        "profitability_ltc": "0",
        "algo": 18,
        "speed": "0"
      },
      {
        "profitability_above_ltc": "0",
        "price": "0.0001",
        "profitability_ltc": "0",
        "algo": 19,
        "speed": "0.00016779"
      },
      {
        "price": "0.0059",
        "algo": 20,
        "speed": "8824.36047254",
        "profitability_eth": "0",
        "profitability_above_eth": "0"
      },
      {
        "profitability_above_ltc": "0",
        "price": "0.0187",
        "profitability_ltc": "0",
        "algo": 21,
        "speed": "370612.8438268"
      },
      {
        "profitability_above_ltc": "0",
        "price": "0.1729",
        "profitability_ltc": "0",
        "algo": 22,
        "speed": "0.1064621"
      },
      {
        "profitability_above_ltc": "0",
        "price": "0.0612",
        "profitability_ltc": "0",
        "algo": 23,
        "speed": "36146.26380671"
      },
      {
        "profitability_above_ltc": "0",
        "price": "0.3499",
        "profitability_ltc": "0",
        "algo": 24,
        "speed": "0.10152039"
      },
      {
        "profitability_above_ltc": "0",
        "price": "0.05",
        "profitability_ltc": "0",
        "algo": 25,
        "speed": "26332.59479137"
      },
      {
        "profitability_above_ltc": "0",
        "price": "0.0054",
        "profitability_ltc": "0",
        "algo": 26,
        "speed": "3.70231186"
      },
      {
        "profitability_above_ltc": "0",
        "price": "0.0049",
        "profitability_ltc": "0",
        "algo": 27,
        "speed": "7123.4766245"
      },
      {
        "profitability_above_ltc": "0",
        "price": "0",
        "profitability_ltc": "0",
        "algo": 28,
        "speed": "0"
      },
      {
        "profitability_above_ltc": "0",
        "price": "0.0043",
        "profitability_ltc": "0",
        "algo": 29,
        "speed": "16.33657586"
      }
    ]
  },
  "method": "stats.global.current"
}
Nisarg Shah
  • 14,151
  • 6
  • 34
  • 55

3 Answers3

1

Use Json.NET (Newtonsoft.Json NuGet package).

public class Stat
{
    public string profitability_above_ltc { get; set; }
    public string price { get; set; }
    public string profitability_ltc { get; set; }
    public int algo { get; set; }
    public string speed { get; set; }
    public string profitability_btc { get; set; }
    public string profitability_above_btc { get; set; }
    public string profitability_eth { get; set; }
    public string profitability_above_eth { get; set; }
}

public class Result
{
    public List<Stat> stats { get; set; }
}

public class RootObject
{
    public Result result { get; set; }
    public string method { get; set; }
}

var json = new WebClient().DownloadString("url");

RootObject deserializedProduct = JsonConvert.DeserializeObject<RootObject>(json);

//deserializedProduct.Result[i].price  <-- this is what you want.
David
  • 15,894
  • 22
  • 55
  • 66
  • And where i put the http url from where i get the json data? – Giuseppe Cerutti Mar 02 '18 at 09:15
  • Ok, so the code is this: public class HandlerClass { public class Stat {..} public class Result {..} public class RootObject {..} public static void HandleOrder(ref Order OrderStats, ref double MaxPrice, ref double NewLimit) { var json = new WebClient().DownloadString("url"); if (json == null) return; RootObject deserializedProduct = JsonConvert.DeserializeObject(json); double nprice = deserializedProduct.Result[20].price; MaxPrice = nprice; }} But i get an error on the definiction of Result in HandlerClass.RootObject – Giuseppe Cerutti Mar 02 '18 at 09:29
0

Use Json.Net. Write a class that repsresents a model and use

JsonConvert.DeserializeObject<MyModel>(json);

If needed use inheritance and composition to build powerful model if needed.

there are also other libraries depending on what you want achieve.

bunny1985
  • 762
  • 6
  • 21
0

Follow the steps:

  1. Install Newtonsoft json nuget package to you project. (https://www.nuget.org/packages/newtonsoft.json/)

  2. Convert JSON response (string) to relative classes (http://json2csharp.com/ )

    public class Stat { public string profitability_above_ltc { get; set; } public string price { get; set; } public string profitability_ltc { get; set; } public int algo { get; set; } public string speed { get; set; } public string profitability_btc { get; set; } public string profitability_above_btc { get; set; } public string profitability_eth { get; set; } public string profitability_above_eth { get; set; } }

    public class Result { public List stats { get; set; } }

    public class RootObject { public Result result { get; set; } public string method { get; set; } }

  3. Deserialize json string

    RootObject Product = JsonConvert.DeserializeObject(jsonString);

  4. Loop through data

    Product.Result[i].price <=this is what you want

Gaurang Dave
  • 3,956
  • 2
  • 15
  • 34