JSON是一种与开发语言无关的、轻量级的数据格式。全称JavaScript Object Notation。
创建JSONObject
JSONObject jsonObject = new JSONObject();
jsonObject.put("name","张三");
jsonObject.put("age",23);
jsonObject.put("hobby",new String[]{"打球","学习"});
Object nullObj = null;
jsonObject.put("car",nullObj);
System.out.println(jsonObject.toString());
使用Map构建JSON
Map<String, Object> jsonObject= new HashMap<String, Object>();
jsonObject.put("name","张三");
jsonObject.put("age",23);
jsonObject.put("hobby",new String[]{"打球","学习"});
System.out.println(new JSONObject(jsonObject).toString());//使用构造函数将Map转换成JSONObject
使用Java Bean构建JSON
JsonObj jsonObj = new JsonObj();
jsonObj.setName("张三");
jsonObj.setAge(23);
jsonObj.setHobby(new String[]{"打球","学习"});
System.out.println(new JSONObject(jsonObject).toString());//使用构造函数将java对象转换成JSONObject
从文件读取解析JSON
File file = File(ReadJSONSample.class.getResource("/jsonFile.json").getFile());
String content = FileUtils.readFileToString(file);
JSONObject jsonObject = new JSONObject(content);
System.out.println("姓名:"+jsonObject.getString("name"));
//获取数组
JSONArray majorArray = jsonObject.getJSONArray("major");
//遍历数组
for(int i = 0; i<majorArray.length(); i++){
String m = (String)majorArray.get(i);
}
判断JSON为NULL方法
if(!jsonObject.isNull("name"))
google-gson生成JSON数据
JsonObj jsonObj = new JsonObj();
jsonObj.setName("张三");
jsonObj.setAge(23);
jsonObj.setHobby(new String[]{"打球","学习"});
Gson gson = new Gson();
System.out.println(gson.toJson(jsonObj));
google-gson设置对象属性大写
public class JsonObj{
//注解name属性为大写,调用google-gson生成JSON数据时,该属性的Key为大写的NAME。
@SerializedName("NAME");
private String name;
private String age;
private String[] hobby;
}
JsonObj jsonObj = new JsonObj();
jsonObj.setName("张三");
jsonObj.setAge(23);
jsonObj.setHobby(new String[]{"打球","学习"});
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setPrettyPrinting();
gsonBuilder.setFieldNamingStrategy(new FieldNamingStrategy()){
public String translateName(Field f){
if(f.getName().equals("name")){
return "NAME";
}
return f.getName();
}
}
Gson gson = gsonBuilder.create();
System.out.println(gson.toJson(jsonObj));
google-gson格式化JSON
JsonObj jsonObj = new JsonObj();
jsonObj.setName("张三");
jsonObj.setAge(23);
jsonObj.setHobby(new String[]{"打球","学习"});
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setPrettyPrinting();
Gson gson = gsonBuilder.create();
System.out.println(gson.toJson(jsonObj));//输出格式化的JSON数据
google-gson使用关键字transient设置对象属性生成JSON数据时忽略
public class JsonObj{
private String name;
private transient String age;//关键字transient设置对象属性生成JSON数据时忽略
private String[] hobby;
}
google-gson解析JSON数据生成实体类
File file = File(ReadJSONSample.class.getResource("/jsonFile.json").getFile());
String content = FileUtils.readFileToString(file);
Gson gson = new Gson();
JsonObj jsonObj = gson.fromJson(content, JsonObj.class);
google-gson解析带日期JSON数据生成实体类
File file = File(ReadJSONSample.class.getResource("/jsonFile.json").getFile());
String content = FileUtils.readFileToString(file);
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();//设置日期格式
JsonObj jsonObj = gson.fromJson(content, JsonObj.class);
System.out.println(jsonObj.getBirthday().toLocaleString());