GSON生成与解析JSON数据
来源:互联网 发布:诸葛亮知天下事 编辑:程序博客网 时间:2024/05/23 16:18
代码:
#JSON对应的实体类
package com.east.entity;import java.io.Serializable;import java.util.Arrays;import com.google.gson.annotations.SerializedName;public class User implements Serializable {@SerializedName("Name") //在转换为json格式后变为大写的Name private String name; private String school; private boolean has_girlfriend; private double age; private Object car; private Object house; private String[] major; private String comment; private String birthday; private transient String ignore; //生成json时忽略该字段 public String getIgnore() {return ignore;}public void setIgnore(String ignore) {this.ignore = ignore;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSchool() {return school;}public void setSchool(String school) {this.school = school;}public boolean isHas_girlfriend() {return has_girlfriend;}public void setHas_girlfriend(boolean has_girlfriend) {this.has_girlfriend = has_girlfriend;}public double getAge() {return age;}public void setAge(double age) {this.age = age;}public Object getCar() {return car;}public void setCar(Object car) {this.car = car;}public Object getHouse() {return house;}public void setHouse(Object house) {this.house = house;}public String[] getMajor() {return major;}public void setMajor(String[] major) {this.major = major;}public String getComment() {return comment;}public void setComment(String comment) {this.comment = comment;}public String getBirthday() {return birthday;}public void setBirthday(String birthday) {this.birthday = birthday;}@Overridepublic String toString() {return "User [name=" + name + ", school=" + school + ", has_girlfriend=" + has_girlfriend + ", age=" + age+ ", car=" + car + ", house=" + house + ", major=" + Arrays.toString(major) + ", comment=" + comment+ ", birthday=" + birthday + "]";} }
#要读取的JSON文件(user.json)
{ "Name": "杨旭", "school": "山东蓝翔", "has_girlfriend": false, "age": 22.5, "major": [ "理发", "挖掘机" ], "comment": "这是一个注释!", "birthday": "1995-08-18"}
#三种方式生成JSON数据
package com.east.util.jsonObject;import java.lang.reflect.Field;import java.util.HashMap;import java.util.Map;import com.east.entity.User;import com.google.gson.FieldNamingStrategy;import com.google.gson.Gson;import com.google.gson.GsonBuilder;import net.sf.json.JSONObject;public class JsonObjectUtil {/* * 使用JsonObject生成json数据 */public String JsonObject() {JSONObject json = new JSONObject();Object nullobj = null;try {json.put("name", "杨旭");json.put("age", 22.5);json.put("birthday", "1995-08-18");json.put("school", "山东蓝翔");json.put("major", new String[] { "理发", "挖掘机" });json.put("has_girlfriend", false);json.put("car", nullobj);json.put("house", nullobj);json.put("comment", "这是一个注释!");} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}String JSON = json.toString(); // 将json格式转为字符串return JSON;}/* * 通过HashMap构建JSON数据 */public String CreateJsonByMap() {Map<String, Object> json = new HashMap<String, Object>();Object nullobj = null;json.put("name", "杨旭");json.put("age", 22.5);json.put("birthday", "1995-08-18");json.put("school", "山东蓝翔");json.put("major", new String[] { "理发", "挖掘机" });json.put("has_girlfriend", false);json.put("car", nullobj);json.put("house", nullobj);json.put("comment", "这是一个注释!");return (new JSONObject(json).toString());}/* * 通过JavaBean构建JSON */public String CreateJsonByBean() {User user = new User();user.setName("杨旭");user.setAge(22.5);user.setBirthday("1995-08-18");user.setSchool("山东蓝翔");user.setMajor(new String []{"理发", "挖掘机"});user.setHas_girlfriend(false);user.setCar(null);user.setHouse(null);user.setComment("这是一个注释!");user.setIgnore("不能看见我!");//Gson gson = new Gson();//个性化生成jsonGsonBuilder builder = new GsonBuilder();builder.setPrettyPrinting();//生成json过程中做处理builder.setFieldNamingStrategy(new FieldNamingStrategy(){@Overridepublic String translateName(Field f) {// TODO Auto-generated method stubif(f.getName().equals("name")){return "Name";}return f.getName();}});//个性化创建json,格式化Gson gson = builder.create();String json = gson.toJson(user);return (json);}}
#读取JSON文件(user.json),解析成实体类(User)
package com.east.read;import java.io.File;import java.io.IOException;import java.net.URLEncoder;import org.apache.commons.io.FileUtils;import com.east.entity.User;import com.google.gson.Gson;public class GsonRead {public User getJson() throws IOException{ File file = new File(GsonRead.class.getResource("/user.json").getFile()); String content = FileUtils.readFileToString(file);// content = URLEncoder.encode(content, "UTF-8"); //避免文件编码错误 Gson gson = new Gson(); User user = gson.fromJson(content, User.class); return user;}}
#测试类(Test.java)
package com.east.test;import java.io.IOException;import java.util.HashMap;import java.util.Map;import com.east.entity.User;import com.east.read.GsonRead;import com.east.util.jsonObject.JsonObjectUtil;import net.sf.json.JSONObject;public class Test {public static void main(String[] args) throws IOException {JsonObjectUtil util = new JsonObjectUtil();/* * 使用JsonObject解析成json数据 */String json = util.JsonObject();System.out.println(json);/* * 通过HashMap构建JSON */String json = util.CreateJsonByMap();System.out.println(json);/* * 通过gson创建json */String json = util.CreateJsonByBean();System.out.println(json);/* * GSON解析文件 */GsonRead read = new GsonRead();User user = read.getJson();System.out.println(user);}}
阅读全文
0 0
- GSON生成与解析JSON数据
- GSON方式生成和解析json数据
- GSON方式生成和解析json数据
- google-GSON解析和生成JSON数据
- GSON方式生成和解析json数据
- Android Gson生成、解析json数据
- GSON方式生成和解析json数据
- GSON方式生成和解析json数据
- GSON方式生成和解析json数据
- 使用Gson生成和解析json数据
- GSON方式生成和解析json数据
- Gson解析Json数据
- Gson解析Json数据
- Gson解析Json数据
- Gson解析json数据
- Gson解析json数据
- Gson解析Json数据
- Gson解析Json数据
- Bs总结
- HTTP通信的概念
- 分布式事务
- 拦截导弹
- 半年-足以改变一个人
- GSON生成与解析JSON数据
- Oracle 11g不能导出空表的三种实用解决方法
- Java多线程-- 公平锁(一)
- CSDN下载积分攻略
- python 3.5: TypeError: a bytes-like object is required, not 'str' when writing to a file
- 电路实验
- 2676 3-7 类的友元函数的应用
- Linux下函数库管理
- 苹果