从JSON API中提取数据

时间:2018-03-02 08:35:20

标签: c# json

我需要修改一个软件。 我需要从中提取“价格”数据,我该怎么办?

{
  "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"
}

3 个答案:

答案 0 :(得分:1)

使用Json.NET(Newtonsoft.Json NuGet包)。

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.

答案 1 :(得分:0)

使用Json.Net。写一个代表模型并使用

的类
JsonConvert.DeserializeObject<MyModel>(json);

如果需要,可以根据需要使用继承和组合来构建强大的模型。

还有其他库,具体取决于您想要实现的目标。

答案 2 :(得分:0)

按照以下步骤操作:

  1. 将Newtonsoft json nuget包安装到您的项目中。 (https://www.nuget.org/packages/newtonsoft.json/

  2. 将JSON响应(字符串)转换为相对类(http://json2csharp.com/

    公共类统计 {     public string profitability_above_ltc {get;组; }     公共字符串价格{get;组; }     public string profitability_ltc {get;组; }     public int algo {get;组; }     公共字符串速度{get;组; }     public string profitability_btc {get;组; }     public string profitability_above_btc {get;组; }     public string profitability_eth {get;组; }     public string profitability_above_eth {get;组; } }

    公共类结果 {     公开列表统计信息{get;组; } }

    公共类RootObject {     公共结果结果{get;组; }     公共字符串方法{get;组; } }

  3. 反序列化json字符串

    RootObject Product = JsonConvert.DeserializeObject(jsonString);

  4. 循环数据

    Product.Result [i] .price &lt; =这就是你想要的