jason by gson复习
来源:互联网 发布:智能电视点播软件 编辑:程序博客网 时间:2024/05/16 15:46
把gson-x.x.x.jar添加到工程。
jason读:
test.json
{ "cat":"it", "languages":[ {"id":1,"ide":"eclipse","name":"java"}, {"id":2,"ide":"xcode","name":"swift"}, {"id":3,"ide":"visual studio","name":"c#"} ], "pop":true}
java代码读取json文件:
package test;import java.io.FileNotFoundException;import java.io.FileReader;import com.google.gson.JsonArray;import com.google.gson.JsonIOException;import com.google.gson.JsonObject;import com.google.gson.JsonParser;import com.google.gson.JsonSyntaxException;public class ReadJason { public static void main(String[] args) { try { JsonParser parser= new JsonParser(); JsonObject object=(JsonObject) parser.parse(new FileReader("test.json")); System.out.println("cat="+object.get("cat").getAsString()); System.out.println("pop="+object.get("pop").getAsBoolean()); JsonArray array=object.get("languages").getAsJsonArray(); for(int i=0;i<array.size();i++){ System.out.println("------------"); JsonObject subObject=array.get(i).getAsJsonObject(); System.out.println("id="+subObject.get("id").getAsInt()); System.out.println("name="+subObject.get("name").getAsString()); System.out.println("ide="+subObject.get("ide").getAsString()); } } catch (JsonIOException e) { e.printStackTrace(); } catch (JsonSyntaxException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } }}
结果输出:
cat=itpop=true------------id=1name=javaide=eclipse------------id=2name=swiftide=xcode------------id=3name=c#ide=visual studio
json写出上式test.json文件:注意jre不可过低,否则部分方法出问题。
package test;import com.google.gson.JsonArray;import com.google.gson.JsonObject;public class JsonWrite { public static void main(String[] args) { JsonObject object =new JsonObject(); object.addProperty("cat", "it"); JsonArray array =new JsonArray(); JsonObject lan1=new JsonObject(); lan1.addProperty("id",1); lan1.addProperty("name", "java"); lan1.addProperty("ide", "eclipse"); array.add(lan1); JsonObject lan2=new JsonObject(); lan2.addProperty("id",2); lan2.addProperty("name", "swift"); lan2.addProperty("ide", "xcode"); array.add(lan2); JsonObject lan3=new JsonObject(); lan3.addProperty("id",3); lan3.addProperty("name", "c#"); lan3.addProperty("ide", "visual studio"); array.add(lan3); object.add("languages",array); object.addProperty("pop",true); System.out.println(object.toString()); }}
0 0
- jason by gson复习
- Game Engine Architecture by Jason Gregory: 目录
- Game Engine Architecture by Jason Gregory: 序
- 【Jason's_ACM_解题报告】Partitioning by Palindromes
- Give it five minutes by Jason Fried
- Jason
- Game Engine Architecture by Jason Gregory:1.2 什么是一个游戏
- Game Engine Architecture by Jason Gregory:1.5 游戏引擎概况
- Game Engine Architecture by Jason Gregory:2.1 版本控制(1)
- Game Engine Architecture by Jason Gregory:2.1 版本控制(2)
- jason jason jason
- Jason解析基础、 相关类库以及FastJSON、Gson和Jackson
- jason面试记(1)--你复习好简历了么?
- Game Engine Architecture by Jason Gregory:1.1 一个典型游戏团队的结构
- Game Engine Architecture by Jason Gregory:1.3 什么是一个游戏引擎
- Game Engine Architecture by Jason Gregory:1.4 不同类型的游戏引擎(1)
- Game Engine Architecture by Jason Gregory:1.4 不同类型的游戏引擎(2)
- Game Engine Architecture by Jason Gregory:1.6 实时游戏引擎架构(1)
- HDU 2298 物理题 纯数学
- 如何改变Dialog出现的位置_仿男衣库Dailog
- VS2017 编译64位boost静态库
- Find Mode in Binary Search Tree
- usb中的endpoint(端点)和传输模式
- jason by gson复习
- 下拉菜单控件JComboBox的使用
- ANDROID STUDIO系列教程四--GRADLE基础
- CodeForces 761B Dasha and friends
- ListView显示多种布局样式
- myeclipse快捷键大全
- 文件和表单的简单上传
- 素数求和问题
- java初中级工程师面试笔试题目及心得(全部实战经验)