{"id":1176,"date":"2023-03-16T22:42:31","date_gmt":"2023-03-16T14:42:31","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1176"},"modified":"2023-04-29T09:56:13","modified_gmt":"2023-04-29T01:56:13","slug":"flutter-json-parsing","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/16\/flutter-json-parsing\/","title":{"rendered":"Flutter\u4e2dJson\u89e3\u6790"},"content":{"rendered":"<h2>json.decode<\/h2>\n<p>json.decode\u8fd4\u56de\u503c\u4e3aMap&lt;String, dynamic&gt;\u76f4\u5230\u8fd0\u884c\u7684\u65f6\u5019\u624d\u80fd\u77e5\u9053\u5177\u4f53\u7684\u7c7b\u578b<\/p>\n<p>\u6240\u4ee5\u4e0d\u80fd\uff1a\u7c7b\u578b\u68c0\u67e5\uff0c\u81ea\u52a8\u63d0\u793a\uff0c\u7f16\u8bd1\u65f6\u65e0\u6cd5\u53d1\u73b0\u9519\u8bef<\/p>\n<p><!-- more --><\/p>\n<h2>json_serializable<\/h2>\n<p>\uff081\uff09pubspec.yaml\u4e2d\u6dfb\u52a0\u4f9d\u8d56<\/p>\n<pre><code>dependencies:\n  json_annotation: ^2.0.0  # https:\/\/pub.dartlang.org\/packages\/json_annotation\n\ndev_dependencies:\n  build_runner: ^1.1.2  # https:\/\/pub.dartlang.org\/packages\/build_runner\n  json_serializable: ^2.0.1  # https:\/\/pub.dartlang.org\/packages\/json_serializable<\/code><\/pre>\n<p>\uff082\uff09\u521b\u5efaModel\u5e76\u6dfb\u52a0\u6ce8\u89e3<\/p>\n<pre><code class=\"language-dart\">import &#039;package:json_annotation\/json_annotation.dart&#039;;\n\npart &#039;person.g.dart&#039;;\n\n@JsonSerializable()\nclass Person {\n  String name;\n  int age;\n\n  Person(this.name, this.age);\n\n  factory Person.fromJson(Map&lt;String, dynamic&gt; json) =&gt; _$PersonFromJson(json);\n  Map&lt;String, dynamic&gt; toJson() =&gt; _$PersonToJson(this);\n}<\/code><\/pre>\n<p>\uff083\uff09\u81ea\u52a8\u751f\u6210fromJson\u548ctoJson\uff08model.g.dart\uff09<\/p>\n<pre><code>flutter packages pub run build_runner build<\/code><\/pre>\n<h2>\u4ee3\u7801\u793a\u4f8b<\/h2>\n<pre><code class=\"language-dart\">import &#039;package:flutter\/material.dart&#039;;\nimport &#039;dart:convert&#039;;\n\nimport &#039;package:flutter_app\/model\/person.dart&#039;;\n\nclass JsonDemoPage extends StatefulWidget {\n  @override\n  State&lt;StatefulWidget&gt; createState() =&gt; new _JsonDemoPagePageState();\n}\n\nclass _JsonDemoPagePageState extends State&lt;JsonDemoPage&gt; {\n  String name = &quot;test&quot;;\n  User user = null;\n  Person person = null;\n\n  @override\n  void initState() {\n    super.initState();\n  }\n\n  @override\n  void didUpdateWidget(covariant JsonDemoPage oldWidget) {\n    name = JsonDecoder.parseJson2Map();\n    user = JsonDecoder.parseJson2Object();\n    person = JsonDecoder.parseJsonSerialize();\n  }\n\n  Widget build(BuildContext context) {\n    return Scaffold(\n        appBar: new AppBar(\n          title: new Text(&#039;JSON Demo&#039;),\n        ),\n        body: Center(\n          child: Column(\n            mainAxisAlignment: MainAxisAlignment.center,\n            children: &lt;Widget&gt;[\n              Text(&#039;name: $name&#039;),\n              SizedBox(height: 10,),\n              Text(&#039;User: [name: ${user.name}, homepage: ${user.homepage}]&#039;, textAlign: TextAlign.center,),\n              SizedBox(height: 10,),\n              Text(&#039;Person: ${person.toJson()}&#039;, textAlign: TextAlign.center,),\n            ],\n          )\n        ),\n    );\n  }\n}\n\nclass JsonDecoder {\n\n  \/\/json.decode\u8fd4\u56de\u503c\u4e3aMap&lt;String, dynamic&gt;\u76f4\u5230\u8fd0\u884c\u7684\u65f6\u5019\u624d\u80fd\u77e5\u9053\u5177\u4f53\u7684\u7c7b\u578b\n  \/\/\u6240\u4ee5\u4e0d\u80fd\uff1a\u7c7b\u578b\u68c0\u67e5\uff0c\u81ea\u52a8\u63d0\u793a\uff0c\u7f16\u8bd1\u65f6\u65e0\u6cd5\u53d1\u73b0\u9519\u8bef\uff0c\u4e25\u91cd\u4e0d\u63a8\u8350\uff01\n  static String parseJson2Map() {\n    String jsonStr = &#039;{&quot;name&quot;: &quot;Joe.Ye&quot;}&#039;;\n    var user = json.decode(jsonStr);\n    print(&#039;${user[&#039;name&#039;]}&#039;);\n    return &#039;${user[&#039;name&#039;]}&#039;;\n  }\n\n  \/\/\u624b\u5199fromJson, toJson\u4e0d\u592a\u53cb\u597d\n  static User parseJson2Object() {\n    String jsonStr = &#039;{&quot;name&quot;:&quot;Joe.Ye&quot;, &quot;email&quot;: &quot;yezhou@yezhou.org&quot;, &quot;homepage&quot;:&quot;http:\/\/www.appblog.cn&quot;}&#039;;\n    Map userMap = json.decode(jsonStr);\n    var user = new User.fromJson(userMap);\n    print(&#039;${user.name}&#039;);\n    print(user.toJson());\n    return user;\n  }\n\n  static Person parseJsonSerialize() {\n    String jsonStr = &#039;{&quot;name&quot;:&quot;Joe.Ye&quot;, &quot;age&quot;:30}&#039;;\n    Map personMap = json.decode(jsonStr);\n    return Person.fromJson(personMap);\n  }\n\n  \/\/https:\/\/javiercbk.github.io\/json_to_dart\/\n}\n\nclass User {\n  final String name;\n  final String email;\n  final String homepage;\n\n  User(this.name, this.email, this.homepage);\n\n  User.fromJson(Map&lt;String, dynamic&gt; json)\n      : name = json[&#039;name&#039;],\n        email = json[&#039;email&#039;],\n        homepage = json[&#039;homepage&#039;];\n\n  Map&lt;String, dynamic&gt; toJson() =&gt; {\n    &#039;name&#039;: name,\n    &#039;email&#039;: email,\n    &#039;homepage&#039;: homepage\n  };\n}<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/Flutter\/Flutter_Json.png\" alt=\"Flutter Json\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>json.decode json.decode\u8fd4\u56de\u503c\u4e3aMap&lt;String, dynamic&gt;\u76f4\u5230 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38],"tags":[74],"class_list":["post-1176","post","type-post","status-publish","format-standard","hentry","category-flutter","tag-json"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1176","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1176"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1176\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1176"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1176"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1176"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}