從GitHub下載GSON:https://github.com/google/gson
Gson的應(yīng)用主要為toJson與fromJson兩個轉(zhuǎn)換函數(shù),而在使用這種對象轉(zhuǎn)換之前需先創(chuàng)建好對象的類別以及其成員才能成功的將JSON字符串成功轉(zhuǎn)換成相對應(yīng)的對象。
class Examples { private int answer1 = 100; private String answer2 = "Hello world!"; Examples(){ } // default constructor }序列化JAVA對象成JSON字符串
Examples example1 = new Examples(); Gson gson = new Gson(); String json = gson.toJson(example1);
json結(jié)果將是
{"answer1":100,"answer2":"Hello world!"}反序列化JSON字符串成對應(yīng)的JAVA對象
Examples example2= gson.fromJson(json,Examples.class);
==> example2即 主站蜘蛛池模板: 乌拉特后旗| 白朗县| 定结县| 南通市| 原平市| 蓬莱市| 罗江县| 藁城市| 绥中县| 涡阳县| 随州市| 宁远县| 喜德县| 凤山县| 临湘市| 清远市| 突泉县| 南郑县| 确山县| 永登县| 视频| 登封市| 铜陵市| 临海市| 湖南省| 游戏| 兴国县| 察隅县| 天峻县| 类乌齐县| 安多县| 垫江县| 德化县| 西畴县| 岑巩县| 阿荣旗| 中西区| 长汀县| 深泽县| 安丘市| 佛学|