{"id":4621,"date":"2019-09-13T20:31:52","date_gmt":"2019-09-13T12:31:52","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=4621"},"modified":"2019-09-13T20:31:52","modified_gmt":"2019-09-13T12:31:52","slug":"java%e4%b8%ad%e7%94%a8gson%e8%a7%a3%e6%9e%90json%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/4621.html","title":{"rendered":"Java\u4e2d\u7528Gson\u89e3\u6790json\u5b57\u7b26\u4e32"},"content":{"rendered":"\n<p>=Start=<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"id-\u6a21\u677f-\u7f18\u7531\uff1a\">\u7f18\u7531\uff1a<\/h4>\n\n\n\n<p>\u56e0\u4e3a\u963f\u91cc\u5f00\u6e90\u7684fastjson\u524d\u6bb5\u65f6\u95f4\u7206\u51fa\u7684\u6f0f\u6d1e\u5b9e\u5728\u662f\u592a\u591a\u4e86\uff08\u800c\u4e14\u6709\u4e9b\u6f0f\u6d1e\u4ed6\u4eec\u8fd8\u559c\u6b22\u5077\u5077\u5904\u7406\uff0c\u4e0d\u53ca\u65f6\u901a\u77e5\u5e7f\u5927\u7528\u6237\u3002\u3002\u3002\uff09\uff0c\u867d\u7136\u5b83\u7528\u8d77\u6765\u5f88\u65b9\u4fbf\uff0c\u4f46\u662f\uff0c\u5b89\u5168\u7b2c\u4e00\uff0c\u65e9\u6362gson\u4fdd\u5e73\u5b89\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"id-\u6a21\u677f-\u6b63\u6587\uff1a\">\u6b63\u6587\uff1a<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"id-\u6a21\u677f-\u53c2\u8003\u89e3\u7b54\uff1a\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n\n\n\n<p><a href=\"https:\/\/github.com\/google\/gson\" target=\"_blank\" rel=\"noreferrer noopener\">Gson<\/a>&nbsp;\u63d0\u4f9b\u7b80\u6d01\u7684&nbsp;<code>toJson()<\/code>&nbsp;\u548c&nbsp;<code>fromJson()<\/code>&nbsp;\u65b9\u6cd5\u4ee5\u5728Java\u5bf9\u8c61\u548cjson\u5b57\u7b26\u4e32\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>toJson()<\/code>&nbsp;\u2013 Java object to JSON<\/li><li><code>fromJson()<\/code>&nbsp;\u2013 JSON to Java object<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u4e00\u4e9b\u5e38\u7528\u7684Demo\u5c55\u793a\uff0c\u65b9\u4fbf\u4ee5\u540e\u4f7f\u7528\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">package com.ixyzero.learn.utils;<br><br>import com.google.gson.Gson;<br>import com.google.gson.GsonBuilder;<br>import com.google.gson.reflect.TypeToken;<br><br>import java.io.FileNotFoundException;<br>import java.io.FileReader;<br>import java.io.FileWriter;<br>import java.io.IOException;<br>import java.util.List;<br>import java.util.Map;<br><br>\/**<br> * Created by ixyzero on 2019\/9\/13.<br> *\/<br>public class LearnGson {<br>    public static void main(String[] args) {<br>        \/\/ Gson gson = new Gson();<br><br>        \/\/ pretty print<br>        Gson gson = new GsonBuilder().setPrettyPrinting().create();<br><br>        \/\/ \u5c06 JSON \u5b57\u7b26\u4e32\u8f6c\u6362\u6210 Map \u5bf9\u8c61<br>        String json = \"{\\\"name\\\":\\\"ixyzero.com\\\", \\\"age\\\":11}\";<br>        Map&lt;String, Object&gt; map = gson.fromJson(json, new TypeToken&lt;Map&lt;String, Object&gt;&gt;() {}.getType());<br><br>        \/\/ JDK 1.8 \u652f\u6301\u7684 Lambda \u8868\u8fbe\u5f0f<br>        map.forEach((x, y) -&gt; System.out.println(\"key : \" + x + \" , value : \" + y));<br><br>        \/\/ \u5e38\u89c4 for \u5faa\u73af\u6253\u5370 Map<br>        for (String key : map.keySet()) {<br>            System.out.println(\"Key: \" + key + \", Value: \" + map.get(key));<br>        }<br>        System.out.println();<br><br><br>        \/\/ \u5c06 JSON \u6587\u4ef6\u8f6c\u6362\u6210 Map \u5bf9\u8c61<br>        try {<br>            map = gson.fromJson(new FileReader(\"\/path\/to\/input.json\"), new TypeToken&lt;Map&lt;String, Object&gt;&gt;() {}.getType());<br>        } catch (FileNotFoundException e) {<br>            e.printStackTrace();<br>        }<br><br>        \/\/ JDK 1.8 \u652f\u6301\u7684 Lambda \u8868\u8fbe\u5f0f<br>        map.forEach((x, y) -&gt; System.out.println(\"key : \" + x + \" , value : \" + y));<br>        System.out.println();<br><br><br>        \/\/ \u5c06 JSON \u6570\u7ec4\u8f6c\u6362\u6210 List \u5bf9\u8c61<br>        json = \"[{\\\"name\\\":\\\"ixyzero.com\\\"}, {\\\"name\\\":\\\"test\\\"}]\";<br>        List&lt;Map&lt;String, String&gt;&gt; list = gson.fromJson(json, new TypeToken&lt;List&lt;Map&lt;String, String&gt;&gt;&gt;() {}.getType());<br><br>        list.forEach(x -&gt; System.out.println(x));<br>        System.out.println();<br><br>        for (int i = 0; i &lt; list.size(); i++) {<br>            System.out.println( list.get(i) );<br>            list.get(i).forEach((x, y) -&gt; System.out.println(\"key : \" + x + \" , value : \" + y));<br>            System.out.println();<br>        }<br><br>        \/\/ \u5c06 Java \u5bf9\u8c61\u5b58\u5165\u6587\u4ef6\uff08\u501f\u52a9 GsonBuilder().setPrettyPrinting().create() \u8fdb\u884c\u683c\u5f0f\u5316\uff09<br>        try (FileWriter writer = new FileWriter(\"\/path\/to\/output.json\")) {<br>            gson.toJson(map, writer);<br>        } catch (IOException e) {<br>            e.printStackTrace();<br>        }<br><br>        System.out.println();<br>        \/\/ \u5c06 Java \u5bf9\u8c61\u8f6c\u6362\u6210 json \u5b57\u7b26\u4e32<br>        String output = gson.toJson(map);<br>        System.out.println(output);<br>    }<br>}<br><br>\/**<br> * content of 'input.json' *<br><br>{<br>\"name\": \"11-27-gson-deserialization\",<br>\"version\": \"1.0.0\",<br>\"description\": \"\",<br>\"main\": \"index.js\",<br>\"scripts\": {<br>\"prestart\": \"javac -classpath lib\/gson-2.8.0.jar TestMain.java\",<br>\"start\": \"java -classpath .:lib\/gson-2.8.0.jar TestMain\"<br>},<br>\"license\": \"MIT\"<br>}<br> *\/<\/pre>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"id-\u6a21\u677f-\u53c2\u8003\u94fe\u63a5\uff1a\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/mp.weixin.qq.com\/s\/zkOJ9DUJr4GOgplTWAqsvw\">FastJson \u62d2\u7edd\u670d\u52a1\u653b\u51fb\u9884\u8b66\uff08fastjson &lt; 1.2.60\u7248\u672c\uff09<\/a><\/li><li><a href=\"https:\/\/mp.weixin.qq.com\/s\/loabXX0b0WhNe-QiUz7Y7g\">\u3010\u6f0f\u6d1e\u9884\u8b66\u3011fastjson\u8fdc\u7a0b\u62d2\u7edd\u670d\u52a1\u6f0f\u6d1e\u9884\u8b66\u901a\u544a<\/a><\/li><li><a href=\"https:\/\/mp.weixin.qq.com\/s\/tDxJbbnY_bddz5IbNalcRg\">Fastjson\u5b89\u5168\u6f0f\u6d1e\u539f\u7406\u7b80\u6790<\/a><\/li><li>Gson \u2013 How to parse JSON<br><a href=\"https:\/\/www.mkyong.com\/java\/how-to-parse-json-with-gson\/\">https:\/\/www.mkyong.com\/java\/how-to-parse-json-with-gson\/<\/a><\/li><li><a href=\"https:\/\/mvnrepository.com\/artifact\/com.google.code.gson\/gson\">https:\/\/mvnrepository.com\/artifact\/com.google.code.gson\/gson<\/a><\/li><li><a href=\"https:\/\/stackoverflow.com\/questions\/5490789\/json-parsing-using-gson-for-java\">https:\/\/stackoverflow.com\/questions\/5490789\/json-parsing-using-gson-for-java<\/a><\/li><li><a href=\"https:\/\/www.javadoc.io\/doc\/com.google.code.gson\/gson\/2.8.5\">https:\/\/www.javadoc.io\/doc\/com.google.code.gson\/gson\/2.8.5<\/a><\/li><li><a href=\"https:\/\/static.javadoc.io\/com.google.code.gson\/gson\/2.8.5\/com\/google\/gson\/JsonElement.html\">https:\/\/static.javadoc.io\/com.google.code.gson\/gson\/2.8.5\/com\/google\/gson\/JsonElement.html<\/a><\/li><li><a href=\"http:\/\/csbun.github.io\/blog\/2016\/11\/gson-deserialization\/\">Java \u4e2d\u4f7f\u7528 Gson \u53cd\u5e8f\u5217\u5316 JSON \u6570\u636e<\/a><\/li><\/ul>\n\n\n\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u56e0\u4e3a\u963f\u91cc\u5f00\u6e90\u7684fastjson\u524d\u6bb5\u65f6\u95f4\u7206\u51fa\u7684\u6f0f\u6d1e\u5b9e\u5728\u662f\u592a\u591a\u4e86\uff08\u800c\u4e14\u6709\u4e9b\u6f0f\u6d1e\u4ed6\u4eec\u8fd8\u559c\u6b22\u5077 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7],"tags":[1500,1499,61,181],"class_list":["post-4621","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","tag-fastjson","tag-gson","tag-java","tag-json"],"views":10112,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4621","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=4621"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4621\/revisions"}],"predecessor-version":[{"id":4622,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4621\/revisions\/4622"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=4621"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=4621"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=4621"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}