从JSON API Flutter获取项目 [英] Get item from json api flutter
本文介绍了从JSON API Flutter获取项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从此json格式获取"ename"或"job".
How get 'ename' or 'job' from this json format eg.
{"items":[{"empno":7839,"ename":"KING","job":"PRESIDENT","mgr":null,....
对于这种json格式,请使用:
For this json format use:
[
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
例如标题:文本(帖子[index] ['name'])
eg. title: Text(posts[index]['name'])
如何处理项目"
推荐答案
您的解决方案是posts['items']['index']['ename']
,
但是我建议使用以下方法
根据您的回复创建模型类, 例如
Create a model class from your response, for example
class Welcome {
String status;
String error;
int time;
Welcome({this.status, this.error, this.time});
factory Welcome.fromJson(Map<String, dynamic> json) {
return Welcome(
status: json['status'],
error: json['error'],
time: json['time'],
);
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['status'] = this.status;
data['error'] = this.error;
data['time'] = this.time;
return data;
}
}
然后将json转换为这样的模型,
Then convert json to model like this,
Welcome welcome = Welcome.fromJson(dataConvertedToJSON);
现在您可以通过调用其吸气剂来获取数据,
Now you can get data by calling its getters,
welcome.status
要将json转换为模型,可以使用此工具
For converting json to model u can use this tool
这篇关于从JSON API Flutter获取项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文