如何在 Flutter 中解码 JSON?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51601519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to decode JSON in Flutter?
提问by Notheros
How to decode JSON in Flutter?
如何在 Flutter 中解码 JSON?
The question is simple, but the answer isn't, at least for me.
问题很简单,但答案并非如此,至少对我而言。
I have a project that uses a lot of JSON Strings. Basically, the entire communication between the app and the server is through JSON.
我有一个使用大量 JSON 字符串的项目。基本上,应用程序和服务器之间的整个通信都是通过 JSON 进行的。
I have been using JSON.decode(json_string)to deal with it, but today I updated the Flutter core (0.5.8-pre.178) and JSON.decode isn't available anymore.
我一直在使用JSON.decode(json_string)它来处理它,但是今天我更新了 Flutter 核心(0.5.8-pre.178)并且 JSON.decode 不再可用了。
I went to the Flutter Docsto seek help, but it still says to use JSON.decode.
我去Flutter Docs寻求帮助,但它仍然说要使用 JSON.decode。
So, how to decode JSON in Flutter from now on?
那么,从现在开始如何在 Flutter 中解码 JSON?
回答by Suragch
You will need to import dart:convert:
您将需要导入dart:convert:
import 'dart:convert';
Inline example
内联示例
String rawJson = '{"name":"Mary","age":30}';
Map<String, dynamic> map = jsonDecode(rawJson); // import 'dart:convert';
String name = map['name'];
int age = map['age'];
Person person = Person(name, age);
Note:When I was doing this in VS Code for server side Dart I had to specify the type:
注意:当我在 VS Code 中为服务器端 Dart 执行此操作时,我必须指定类型:
Map<String, dynamic> map = jsonDecode(rawJson) as Map<String, dynamic>;
Model class example
模型类示例
The model class includes the map conversion logic:
模型类包含地图转换逻辑:
class Person {
String name;
int age;
Person(this.name, this.age);
// named constructor
Person.fromJson(Map<String, dynamic> json)
: name = json['name'],
age = json['age'];
// method
Map<String, dynamic> toJson() {
return {
'name': name,
'age': age,
};
}
}
And the JSON conversion is done like this:
JSON 转换是这样完成的:
String rawJson = '{"name":"Mary","age":30}';
Map<String, dynamic> map = jsonDecode(rawJson);
Person person = Person.fromJson(map);
See my full answer here.
在此处查看我的完整答案。
Generating the serialization code
生成序列化代码
It is easy to make errors when writing the serialization code, so it is generally recommended to use the json_serializablepackage by the Dart Team. However, you can read about the pros and cons of the different methods here.
写序列化代码的时候很容易出错,所以一般推荐使用Dart Team的json_serializable包。但是,您可以在此处阅读不同方法的优缺点。
If you want even more options you can also check out the built_valuepackage.
如果您想要更多选项,您还可以查看built_value包。
See also
也可以看看
回答by Günter Z?chbauer
Just use
只需使用
json.decode()
or
或者
jsonDecode()
In Dart 2 all screaming-case constants were changed to lower-camel-case.
在 Dart 2 中,所有尖叫大小写的常量都更改为小写字母。
回答by Sanjayrajsinh
You need to use import 'dart:convert';
你需要使用 import 'dart:convert';
Decode :
JsonDecoder().convert("$response");Encode :
JsonEncoder().convert(object)
解码:
JsonDecoder().convert("$response");编码:
JsonEncoder().convert(object)

