json - 如何在飞镖中打印 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']
解决方案
解析完 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"
}
}
}
]
}
''';
推荐阅读
- python - 如何在 BLE(低功耗蓝牙)中有 2 个广告?
- json - 为什么我得到 Unhandled Exception: DatabaseException(datatype mismatch) 虽然一切看起来都很完美?
- html - 嵌入的 Google 表格会删除之后的元素
- c++ - 编译时计算 (C++ v. C)
- r - 最好/更快?在R中反转多对多关系列表的方法
- stored-procedures - 使用 Teradata 存储过程的倒三角形
- python - 基于单独列 pandas 中的标准的累积最大值/最小值
- python - Testcontainers Postgresql 模拟不起作用
- python - 上传到 azure blob 存储的文件为空或什至不存在
- javascript - Reactjs:如何在点击时将对话框移动到屏幕顶部