Flutter 在flatter中解析JSON数组
我的JSON响应是Flutter 在flatter中解析JSON数组,flutter,Flutter,我的JSON响应是 [ { "0":"CSE", "branch":"CSE", "1":"1", "count(`branch`)":"1" }, { "0":"ECE", "branch":"ECE", "1":"2", "count(`branch`)":"2" }, { "0":"IT", "branch":"IT", "1":"1", "count(`branch`)":"1" } ] 我希望在Fulturebuilder中像下面这样输出 BRANCH COUNT
[
{
"0":"CSE",
"branch":"CSE",
"1":"1",
"count(`branch`)":"1"
},
{
"0":"ECE",
"branch":"ECE",
"1":"2",
"count(`branch`)":"2"
},
{
"0":"IT",
"branch":"IT",
"1":"1",
"count(`branch`)":"1"
}
]
我希望在Fulturebuilder中像下面这样输出
BRANCH COUNT
CSE 1
ECE 2
IT 1
我试着用下面的方法来得到那根树枝。但是没有运气。请帮帮我
ListTile( title:new Text('${snapshot.data[index].branch.toString()}),
JSON是一个字符串。您必须首先使用
json对其进行转换。例如,decode
。json是一个字符串。您必须首先使用json对其进行转换。例如,解码。尝试
ListTile( title:new Text('${snapshot.data[index][branch]}),
但我会推荐我解释过的方法
ListTile( title:new Text('${snapshot.data[index][branch]}),
但是我建议您使用我解释过的方法,因为您的json数据是列表格式的,您可以通过下面的代码轻松地完成
import 'package:flutter/material.dart';
class DemoPage extends StatelessWidget {
var response = [
{"0": "CSE", "branch": "CSE", "1": "1", "count(`branch`)": "1"},
{"0": "ECE", "branch": "ECE", "1": "2", "count(`branch`)": "2"},
{"0": "IT", "branch": "IT", "1": "1", "count(`branch`)": "1"}
];
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView.builder(
itemCount: response.length,
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text(response[index]["branch"]),
subtitle: Text(response[index]["count(`branch`)"]),
);
},
),
);
}
}
另外,如果您使用的是来自网络的响应,那么您应该使用json.decode(response)格式化您的响应;然后,您可以按照上面的代码进行操作。由于您的json数据是列表格式的,因此您可以通过以下代码轻松地进行操作
import 'package:flutter/material.dart';
class DemoPage extends StatelessWidget {
var response = [
{"0": "CSE", "branch": "CSE", "1": "1", "count(`branch`)": "1"},
{"0": "ECE", "branch": "ECE", "1": "2", "count(`branch`)": "2"},
{"0": "IT", "branch": "IT", "1": "1", "count(`branch`)": "1"}
];
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView.builder(
itemCount: response.length,
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text(response[index]["branch"]),
subtitle: Text(response[index]["count(`branch`)"]),
);
},
),
);
}
}
另外,如果您使用的是来自网络的响应,那么您应该使用json.decode(response)格式化您的响应;然后您可以按照上面的代码进行操作。您可以将JSON解析为这样的对象 使用此web将字符串解析为对象
https://app.quicktype.io/
将JSON解析为对象所需的代码如下
// To parse this JSON data, do
//
// final exampleStackOverflow = exampleStackOverflowFromJson(jsonString);
import 'dart:convert';
List<ExampleStackOverflow> exampleStackOverflowFromJson(String str) =>
List<ExampleStackOverflow>.from(json.decode(str).map((x) =>
ExampleStackOverflow.fromJson(x)));
String exampleStackOverflowToJson(List<ExampleStackOverflow> data) =>
json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
class ExampleStackOverflow {
String the0;
String the1;
String branch;
String countBranch;
ExampleStackOverflow({
this.the0,
this.the1,
this.branch,
this.countBranch,
});
factory ExampleStackOverflow.fromJson(Map<String, dynamic> json) =>
ExampleStackOverflow(
the0: json["0"],
the1: json["1"],
branch: json["branch"],
countBranch: json["count(`branch`)"],
);
Map<String, dynamic> toJson() => {
"0": the0,
"1": the1,
"branch": branch,
"count(`branch`)": countBranch,
};
}
之后,为了获得输出,只需要从对象调用数据。在我看来,像这样使用JSON要容易得多
您应该使调用JSON的方法返回Future而不是String,这样您就可以执行myObject=snapshot.data;
最后:
ListTile( title:new Text(MyObject.branch),
如果您收到多个对象,只需返回一个列表即可 您可以将JSON解析为这样的对象 使用此web将字符串解析为对象
https://app.quicktype.io/
将JSON解析为对象所需的代码如下
// To parse this JSON data, do
//
// final exampleStackOverflow = exampleStackOverflowFromJson(jsonString);
import 'dart:convert';
List<ExampleStackOverflow> exampleStackOverflowFromJson(String str) =>
List<ExampleStackOverflow>.from(json.decode(str).map((x) =>
ExampleStackOverflow.fromJson(x)));
String exampleStackOverflowToJson(List<ExampleStackOverflow> data) =>
json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
class ExampleStackOverflow {
String the0;
String the1;
String branch;
String countBranch;
ExampleStackOverflow({
this.the0,
this.the1,
this.branch,
this.countBranch,
});
factory ExampleStackOverflow.fromJson(Map<String, dynamic> json) =>
ExampleStackOverflow(
the0: json["0"],
the1: json["1"],
branch: json["branch"],
countBranch: json["count(`branch`)"],
);
Map<String, dynamic> toJson() => {
"0": the0,
"1": the1,
"branch": branch,
"count(`branch`)": countBranch,
};
}
之后,为了获得输出,只需要从对象调用数据。在我看来,像这样使用JSON要容易得多
您应该使调用JSON的方法返回Future而不是String,这样您就可以执行myObject=snapshot.data;
最后:
ListTile( title:new Text(MyObject.branch),
如果您收到多个对象,只需返回一个列表即可 我明白了。谢谢,我知道了。非常感谢。