首页 > 解决方案 > 如何从 Reqwest 响应中获取 API 的 JSON 数据?

问题描述

我想从 API 获取数据:

extern crate reqwest;

use std::io::Read;

pub fn main() {
    let mut response = reqwest::get("https://api.fcoin.com/v2/market/ticker/ftbtc")
        .expect("Failed to send request");
    let mut buf = String::new();
    response
        .read_to_string(&mut buf)
        .expect("Failed to read response");
    println!("{}", buf);
}

输出是

{
  "status": 0,
  "data": {
    "ticker": [
      0.00006173,
      500,
      0.00006173,
      44411.28,
      0.00006174,
      917.98,
      0.00008033,
      0.00008654,
      0.00006026,
      7144217252.9,
      554239.7346328925
    ],
    "type": "ticker.ftbtc",
    "seq": 2995173
  }
}

我可以将bufor 响应转换为 key => value 样式Vec或数组吗?

标签: rustreqwest

解决方案


推荐阅读