简体   繁体   English

我将如何在 Dart 中访问此 JSON 数据以用于 Flutter 项目?

[英]How would I access this JSON data in Dart for a flutter project?

This is the JSON data and I'm trying to access the date and the open value with this code but I'm getting errors.这是 JSON 数据,我正在尝试使用此代码访问日期和打开值,但出现错误。 What would the proper syntax be?正确的语法是什么?

factory Stock.fromJson(Map<String, dynamic> json) { // parse the json into data we can use
    return Stock(
      date: json['Time Series (Daily)[0]'],
      open: json['Time Series (Daily[0].open'],
      high: json['Time Series (Daily[0].high'],
      low: json['Time Series (Daily[0].low'],
      close: json['Time Series (Daily[0].close'],
      volume: json['Time Series (Daily[0].volume']
    );
  }
{
    "Meta Data": {
        "1. Information": "Daily Prices (open, high, low, close) and Volumes",
        "2. Symbol": "MSFT",
        "3. Last Refreshed": "2020-03-26",
        "4. Output Size": "Compact",
        "5. Time Zone": "US/Eastern"
    },
    "Time Series (Daily)": {
        "2020-03-26": {
            "1. open": "148.4000",
            "2. high": "156.6600",
            "3. low": "148.3700",
            "4. close": "155.8800",
            "5. volume": "64143669"
        }
}

To get json objects or values of your json data, you need to pass attributes names and manipulate the json object要获取 json 对象或 json 数据的值,您需要传递属性名称并操作 json 对象

factory Stock.fromJson(Map<String, dynamic> json) { // parse the json into data we can use
return Stock(
  date: json['Time Series (Daily)']['2020-03-26'],
  open: json['Time Series (Daily)']['2020-03-26']['1. open'],
  ...
);

} }

You can also convert your json to dart object with this tool : https://javiercbk.github.io/json_to_dart/您还可以使用此工具将 json 转换为 dart 对象: https : //javiercbk.github.io/json_to_dart/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM