【问题标题】:Flutter: Access data from JSON file & convert it to ObjectFlutter:从 JSON 文件访问数据并将其转换为 Object
【发布时间】:2018-09-10 19:01:29
【问题描述】:

我已经关注存储在 Assets/JSON/example.json 中的 Json

[
 {"optiontext" : "One", "optionvalue" : "One"},
 {"optiontext" : "Two", "optionvalue" : "Two"},
 {"optiontext" : "Three", "optionvalue" : "Three"}
]

我想从此文件中读取 JSON 并将其转换为 MyObject。 必须在 Flutter App 中使用

【问题讨论】:

标签: dart flutter


【解决方案1】:

这是您的案例的示例代码 sn-p。基本上,我们可以使用 dartson 包进行转换。

import 'package:dartson/dartson.dart';

void main(List<String> args) {
  String jsonString = '[{"optiontext" : "One", "optionvalue" : "One"},{"optiontext" : "Two", "optionvalue" : "Two"},{"optiontext" : "Three", "optionvalue" : "Three"}]';

  var dson = new Dartson.JSON();
  List<MyObject> result = dson.decode(jsonString, new MyObject(), true);
  print(result[1].optionValue);
}

@Entity()
class MyObject {
  @Property(name:"optiontext")
  String optionText;
  @Property(name:"optionvalue")
  String optionValue;
}

由于某些问题你不能在flutter中使用dartson,下面的代码sn-p可以在dart:convert的帮助下使用

import 'dart:convert';

void main(List<String> args) {
  String jsonString = '[{"optiontext" : "One", "optionvalue" : "Value"},{"optiontext" : "Two", "optionvalue" : "Two"},{"optiontext" : "Three", "optionvalue" : "Three"}]';
  List<Map> parsedJson = JSON.decode(jsonString);
  List<MyObject> result = parsedJson.map((item) => new MyObject.fromJson(item)).toList();
  print(result[0].optionText);
  print(result[0].optionValue);
}

class MyObject {
  String optionText;
  String optionValue;

  MyObject.fromJson(Map jsonMap)
      : optionText = jsonMap['optiontext'],
        optionValue = jsonMap['optionvalue'];
}

【讨论】:

  • 我想在 Flutter App 中实现它。而且我认为不能在 Flutter 中使用 dartson ......这是链接 - github.com/eredo/dartson/issues/38
  • 对不起,让我更新我的答案只用dart:convert
  • JsonObject 怎么样?它更干净。另一个有用的library
  • 谢谢@RaoufRahiche。目前我主要坚持使用内置飞镖包。很高兴了解 JsonObject。
  • 截至 2020 年 JsonObject 已有 5 年没有更新,我在 marketplace 中找不到它。不过似乎还有很多其他的包......
【解决方案2】:

这是我的解决方案

我有以下 json

[
  {
    "name": "Abhijit Sawant",
    "age": "23",
    "height":"180",
    "gender": "male",
    "hair_color": "black"
  },
  {
    "name": "Akku Sawant",
    "age": "23",
    "height":"150",
    "gender": "male",
    "hair_color": "brown"
  },
  {
    "name": "Pikki Sawant",
    "age": "23",
    "height":"120",
    "gender": "male",
    "hair_color": "grey"
  },
  {
    "name": "Chikki Sawant",
    "age": "23",
    "height":"100",
    "gender": "female",
    "hair_color": "white"
  }

]

以下是我的 Flutter 代码

class HomePage extends StatefulWidget {

  @override
  State<StatefulWidget> createState() {
    return HomePageState();
  }

}

class HomePageState extends State<HomePage>{

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Loading Json"),),
      body: Container(
        child: Center(
          child: FutureBuilder(builder: (context,snapshot){
            var myData = json.decode(snapshot.data.toString());
            return new ListView.builder(
              itemCount: myData == null ? 0: myData.length,
              itemBuilder: (BuildContext context,int index){
              return Card(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.stretch,
                  children: <Widget>[
                    Text("Name: "+myData[index]["name"]),
                    Text("Age: "+myData[index]["age"]),
                    Text("Height: "+myData[index]["height"]),
                    Text("Gender: "+myData[index]["gender"]),
                  ],
                ),
              );
            },);
          },
          future: DefaultAssetBundle.of(context).loadString("person.json"),),

        ),
      ),
    );
  }

}

【讨论】:

    猜你喜欢
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 2014-11-23
    • 1970-01-01
    • 2020-08-18
    • 2018-12-15
    • 1970-01-01
    • 2012-09-13
    相关资源
    最近更新 更多