颤动:从JSON文件访问数据&将其转换为Object

时间:2018-04-01 08:55:53

标签: dart flutter

我跟随存储在Assets / JSON / example.json

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

我想从这个文件中读取JSON&将其转换为MyObject。 必须在Flutter App中使用它

2 个答案:

答案 0 :(得分:1)

以下是您案例的示例代码段。基本上,我们可以使用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;
}

由于某些问题,您无法在抖动中使用dartson,因此可以在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'];
}

答案 1 :(得分:0)

这是我的解决方法

我有以下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"),),

        ),
      ),
    );
  }

}