【问题标题】:How to get HTTP request from JSON API to a table如何从 JSON API 获取 HTTP 请求到表
【发布时间】:2018-12-19 07:19:11
【问题描述】:

当前示例代码:

  child: Padding(
          padding: const EdgeInsets.all(14.0),
          child: Table(
            border: TableBorder.all(width: 1.0, color: Colors.black),
            children: [
              TableRow(children: [
                TableCell(
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: <Widget>[
                      new Text('Company Symbol'),
                      new Text(data['symbol'].toString()),
                    ],
                  ),
                )
              ]),
              TableRow(children: [
                TableCell(
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: <Widget>[
                      new Text('Name of Company'),
                      new Text(data['companyName'].toString()),
                    ],
                  ),
                )
              ]),
              TableRow(children: [
                TableCell(
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: <Widget>[
                      new Text('Exchnage'),
                      new Text(data['exchange'].toString()),
                    ],
                  ),
                )
              ]),
              TableRow(children: [
                TableCell(
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: <Widget>[
                      new Text('Industry'),
                      new Text(data['industry'].toString()),
                    ],
                  ),
                )
              ])
            ],
          ),
        ),
      ),
    ],
  ),
);
 }
 }'



Future<Map> loadData() async {
http.Response response = await http.get(
    Uri.encodeFull("https://api.iextrading.com/1.0/stock/aapl/company"),
    headers: {"Accept": "application/json"});
Map data = jsonDecode(response.body);
return data;
}

我的问题是我无法检索/获取嵌套在 json/raw 视图中的 json 值。 如果我使用:https://api.iextrading.com/1.0/stock/market/batch?symbols=aapl,fb&types=quote,news,chart&range=1m&last=5 作为网址,我无法获得相同的值。 我需要什么价值观? 例子: new Text(data['symbol'].toString()),

获取符号值:

{"quote":{"symbol":"AAPL","companyName":"Apple Inc.","primaryExchange":"Nasdaq Global Select","sector":"技术","calculationPrice":"close","open":165.42,"openTime":1545057000854,"close":163.94,"closeTime":1545080400475,"high":168.35,"low":162.73,"latestPrice": 163.94,"latestSource":"关闭","latestTime":"2018 年 12 月 17 日","latestUpdate":1545080400475,"latestVolume":42949358,"iexRealtimePrice":null,"iexRealtimeSize":null,"iexLastUpdated":null ,"delayedPrice":164.015,"delayedPriceTime":1545080393722,"extendedPrice":164.09,"extendedChange":0.15,"extendedChangePercent":0.00091,"extendedPriceTime":1545083995658,"previousClose":165.48,"change":-1.54, “更改百分比”:-

【问题讨论】:

  • 如果您只想获取符号,您可以将 JSON 对象存储在变量中,然后这会给您返回符号的值:myObj.AAPL.quote.symbol(myObj 是变量您存储对象的位置)

标签: javascript dart flutter


【解决方案1】:

这是因为首先您需要提取 AAPLquote json 对象。

用这个替换你的方法,它应该可以工作:

    Future<Map> loadData() async {
    http.Response response = await http.get(
        Uri.encodeFull("https://api.iextrading.com/1.0/stock/aapl/company"),
        headers: {"Accept": "application/json"});
    Map data = json.decode(response.body);
    Map quote = data["AAPL"]["quote"];
    return quote;
    }

更新:

如果您想循环获取所有数据,可以在下面查看此方法:

          _loadData() async {
            final response = await http.get("https://api.iextrading.com/1.0/stock/market/batch?symbols=aapl,fb&types=quote,news,chart&range=1m&last=5");
            final Map<String,dynamic> data = json.decode(response.body);
            List dataList = List();
            data.keys.forEach((key){
               dataList.add(data[key]["quote"]);
            });

            dataList.forEach((data){
               final symbol = data["symbol"];
               final companyName = data["companyName"];
               print("symbol :$symbol , companyName : $companyName");
            });

            return dataList;
          }

现在您有了一个dataList 变量,您可以使用它来构建您的小部件。

【讨论】:

猜你喜欢
  • 2021-05-28
  • 1970-01-01
  • 2017-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-22
  • 2017-04-04
  • 1970-01-01
相关资源
最近更新 更多