首页 > 解决方案 > 如何在飞镖中打印 JSON?

问题描述

这是我的 JSON 文件,所以如果我想打印percent_change_1h它的飞镖代码是什么?

    {
        "data": [
           {
                "id": 1,
                "name": "Bitcoin",
                "symbol": "BTC",
                "slug": "bitcoin",
                "cmc_rank": 5,
                "num_market_pairs": 500,
                "circulating_supply": 16950100,
                "total_supply": 16950100,
                "max_supply": 21000000,
                "last_updated": "2018-06-02T22:51:28.209Z",
                "date_added": "2013-04-28T00:00:00.000Z",
                "tags": [
                "mineable"
                ],
                "platform": null,
                "quote": {
                   "USD": {
                       "price": 9283.92,
                       "volume_24h": 7155680000,
                       "percent_change_1h": -0.152774,
                       "percent_change_24h": 0.518894,
                       "percent_change_7d": 0.986573,
                       "market_cap": 158055024432,
                       "last_updated": "2018-08-09T22:53:32.000Z"
                    },
                    "BTC": {
                       "price": 1,
                       "volume_24h": 772012,
                       "percent_change_1h": 0,
                       "percent_change_24h": 0,
                       "percent_change_7d": 0,
                       "market_cap": 17024600,
                       "last_updated": "2018-08-09T22:53:32.000Z"
                    }
                }
           },
        ],
    }

这是代码

Future getCurrencies() async {
    String cryptoUrl = "https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest";

    http.Response response = await http.get(Uri.parse(cryptoUrl),
     headers: {
       'X-CMC_PRO_API_KEY': 'MY-API',
       "Accept": "application/json",
     });

    return jsonDecode(response.body)['data'];
}

这是堆栈跟踪
#0 和 #1 是我打印 percent_change_1h 的地方

The following _TypeError was thrown building:
type 'Null' is not a subtype of type 'String'

When the exception was thrown, this was the stack: 
#0      _HomePageState._getListItemUI (package:crypto_example/home_page.dart:78:44)
#1      _HomePageState._cryptoWidget.<anonymous closure> (package:crypto_example/home_page.dart:49:18)
#2      SliverChildBuilderDelegate.build (package:flutter/src/widgets/sliver.dart:455:22)
#3      SliverMultiBoxAdaptorElement._build (package:flutter/src/widgets/sliver.dart:1213:28)
#4      SliverMultiBoxAdaptorElement.createChild.<anonymous closure> (package:flutter/src/widgets/sliver.dart:1226:55)
...

这是我用于打印的代码 Text(currency['percent_change_1h']

标签: jsonflutterdart

解决方案


解析完 JSON 结构后,您需要遍历它。在您的情况下,它看起来像这样:

import 'dart:convert';

void main() {
  print(jsonDecode(jsonString)['data'][0]['quote']['USD']['percent_change_1h']);
  // -0.152774

  print(jsonDecode(jsonString)['data'][0]['quote']['BTC']['percent_change_1h']);
  // 0
}

const jsonString = '''
{
  "data": [
    {
      "id": 1,
      "name": "Bitcoin",
      "symbol": "BTC",
      "slug": "bitcoin",
      "cmc_rank": 5,
      "num_market_pairs": 500,
      "circulating_supply": 16950100,
      "total_supply": 16950100,
      "max_supply": 21000000,
      "last_updated": "2018-06-02T22:51:28.209Z",
      "date_added": "2013-04-28T00:00:00.000Z",
      "tags": [
        "mineable"
      ],
      "platform": null,
      "quote": {
        "USD": {
          "price": 9283.92,
          "volume_24h": 7155680000,
          "percent_change_1h": -0.152774,
          "percent_change_24h": 0.518894,
          "percent_change_7d": 0.986573,
          "market_cap": 158055024432,
          "last_updated": "2018-08-09T22:53:32.000Z"
        },
        "BTC": {
          "price": 1,
          "volume_24h": 772012,
          "percent_change_1h": 0,
          "percent_change_24h": 0,
          "percent_change_7d": 0,
          "market_cap": 17024600,
          "last_updated": "2018-08-09T22:53:32.000Z"
        }
      }
    }
  ]
}    
''';

推荐阅读