JSON(JavaScript Object Notation)
配置org.json
构建json 1、Map<String,Object> obj = newHashMap<String,Object>; obj.put( , );
2、JsonObject obj =new JsonObject(); obj.put( , );
3、new JsonObject(Bean);
解析Json 1、Sting content =FileUtils.readFileToString(file);
JsonObjectjsonObject=new JsonObject(content);
jsonObject.getString(“KEY值”); jsonObject.getJsonArray(“KEY值”);
配置MappingJackson2JsonView
通过@ResponseBody或者ResponseEntity<>获得返回的Json格式的文件对象
Gson
Gson.toJson(bean);
@SerializedName(“NAME”) 注解在属性上,可将name属性解析成“NAME”
GsonBuilder gsonBuilder= new GsonBuilder();
gsonBuilder.setPrettyPrinting();
gsonBuilder.setFileNamingStrage(new FileNamingStrage(){ 可通过回调函数对Gson操作});
在属性前加transient 可在生成Gson时,不解析此属性
解析Gson Stingcontent =FileUtils.readFileToString(file);
Gson gson=gsonBuilder().setDateFormate(“yyyy-MM-dd”).create();
person per=Gson.fromJson(content,per.class); (直接解析为per对象)
per.getBirthday(); (Gson支持Date和集合类型的属性)