JSON

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());

google-gson集合类解析

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值