如何继续在颤振中接收 JSON 数组并解析它?
Posted
技术标签:
【中文标题】如何继续在颤振中接收 JSON 数组并解析它?【英文标题】:How to go on about receiving JSON array in flutter and parsing it? 【发布时间】:2019-03-05 15:32:58 【问题描述】:我正在尝试从 Web 服务 URL 获取 JSON 数组并用 JSON 解析它。问题是我正在关注的教程显示接收一个 JSOn obj 并解析它,但我需要知道如何接收 JSON 数组并解析它。下面是我正在处理的代码,我卡住了。
型号
class Fact
int id;
int fact_id;
String fact;
String image;
String reference;
Fact(this.id, this.fact_id, this.fact, this.image, this.reference);
Fact.fromJson(Map<String, dynamic> json)
: id = json['id'],
fact_id = json['fact_id'],
fact = json['fact'],
image = json['image'],
reference = json['reference'];
Map<String, dynamic> toJson() =>
'id' : id,
'fact_id': fact_id,
'fact': fact,
'image': image,
'reference': reference,
;
我不知道如何为我从网络服务获得的一系列事实编写这个。
事实下载管理器
class FactsManager
var constants = Constants();
fetchFacts()
final lastFactId = 0;
var fetchRequestUrl = constants.fetch_facts_url;
if (lastFactId == 0)
fetchRequestUrl = fetchRequestUrl + "?count=" + constants.firstTimePostCount.toString();
else
fetchRequestUrl = fetchRequestUrl + "?count=" + constants.firstTimePostCount.toString() + "&last_id=" + lastFactId.toString();
Future<List<Fact>> fetchPost() async
final response = await http.get(fetchRequestUrl);
if (response.statusCode == 200)
return List<Fact>
我试图解析的示例数据。
[
"id": "407",
"fact": "Monsanto once tried to genetically engineer blue cotton, to produce denim without the use of dyes, reducing the pollution involved in the dyeing process. ",
"reference": null,
"image": "http:\/\/quickfacts.me\/wp-content\/uploads\/2015\/06\/fact492.png",
"fact_id": "1"
,
"id": "560",
"fact": "You can count from zero to nine hundred ninety-nine without ever having to use the letter \"a\" ",
"reference": null,
"image": "http:\/\/quickfacts.me\/wp-content\/uploads\/2015\/06\/fact04.png",
"fact_id": "2"
,
"id": "564",
"fact": "In order to keep the project a secret, the British army used the innocuous name \"mobile water carriers\" for a motorized weapons project - which is the reason we call them \"tanks\". ",
"reference": null,
"image": "http:\/\/quickfacts.me\/wp-content\/uploads\/2015\/06\/fact116.png",
"fact_id": "3"
,
"id": "562",
"fact": "In 2010 the mummified corpse of Sogen Kato, thought to be Tokyo's oldest man, was found in his bedroom by government officials. He had actually died in 1978. ",
"reference": null,
"image": "http:\/\/quickfacts.me\/wp-content\/uploads\/2015\/06\/fact216.png",
"fact_id": "4"
,
"id": "566",
"fact": "In 1927 the US Supreme Court ruled it constitutional for the government to forcefully sterilize mentally handicapped people ",
"reference": null,
"image": "http:\/\/quickfacts.me\/wp-content\/uploads\/2015\/06\/fact316.png",
"fact_id": "5"
]
【问题讨论】:
@connexo 好的。我已经编辑了问题并给出了示例数据,那么我该如何解析呢? 【参考方案1】:您可以执行以下操作:
String receivedJson = "... Your JSON string ....";
List<dynamic> list = json.decode(receivedJson);
Fact fact = Fact.fromJson(list[0]);
无论如何,您必须在您的 json 字符串和您制作的 Fact 类中考虑以下内容:
在 json 字符串中,id 和 fact_id 是字符串,您将它们视为 int。您要么更改 json 类,要么更改 Fact 类 json 字符串中的某些字符串会产生错误,因为它们有额外的引号,这会使解码器感到困惑。一个json字符串的作用如下:
String receivedJson = """
[
"id": 407,
"fact": "Monsanto once tried to genetically engineer blue cotton, to produce denim without the use of dyes, reducing the pollution involved in the dyeing process. ",
"reference": null,
"image": "http:\/\/quickfacts.me\/wp-content\/uploads\/2015\/06\/fact492.png",
"fact_id": 1
]
""";
【讨论】:
【参考方案2】:使用如下简单数组:
"tags": [
"dart",
"flutter",
"json"
]
我们可以轻松地将 JSON 解析为 Dart 数组,而无需创建任何类。
import 'dart:convert';
main()
String arrayText = '"tags": ["dart", "flutter", "json"]';
var tagsJson = jsonDecode(arrayText)['tags'];
List<String> tags = tagsJson != null ? List.from(tagsJson) : null;
print(tags);
现在你可以看到打印 Dart Array 的结果了。
[dart, flutter, json]
发件人:Dart/Flutter parse JSON array into List
【讨论】:
【参考方案3】:轻松
String arrayText = '["name": "dart1","quantity": 12 ,"name": "flutter2","quantity": 25 ]';
var tagsJson = jsonDecode(arrayText);
List<dynamic> tags = tagsJson != null ? List.from(tagsJson) : null;
print(">> " + tags[0]["name"]);
print(">> " + tags[1]["name"]);
print(">> " + tags[0]["quantity"].toString());
print(">> " + tags[1]["quantity"].toString());
输出
2021-04-28 18:55:28.921 22085-22225/com.example.flutter_applicationdemo08 I/flutter: >> dart1
2021-04-28 18:55:28.921 22085-22225/com.example.flutter_applicationdemo08 I/flutter: >> flutter2
2021-04-28 18:55:28.921 22085-22225/com.example.flutter_applicationdemo08 I/flutter: >> 12
2021-04-28 18:55:28.921 22085-22225/com.example.flutter_applicationdemo08 I/flutter: >> 25
【讨论】:
以上是关于如何继续在颤振中接收 JSON 数组并解析它?的主要内容,如果未能解决你的问题,请参考以下文章
颤振 - 在 null 上调用了方法“[]”(解析 json)