操作Mysql表的json字段,查询json字段的内容,将新增的内容添加到查询的json数组中
java
String a = "[{\"name\": \"张三\", \"age\": 10, \"gender\": \"男\", \"email\": \"123@qq.com\"}]";
cn.hutool.json.JSONArray objects = JSONUtil.parseArray(a);
System.out.println(a);
JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("name","李四");
jsonObject1.put("age",24);
jsonObject1.put("gender","女");
jsonObject1.put("email","456@qq.com");
JSONObject jsonObject2 = new JSONObject();
jsonObject2.put("name","王五");
jsonObject2.put("age",24);
jsonObject2.put("gender","男");
jsonObject2.put("email","789@qq.com");
objects.add(jsonObject1);
objects.add(jsonObject2);
System.out.println("objects:"+objects);
String jsonStr = JSONUtil.toJsonStr(objects);
System.out.println("jsonStr:"+jsonStr);
输出结果
java
[{"name": "张三", "age": 10, "gender": "男", "email": "123@qq.com"}]
objects:[{"gender":"男","name":"张三","age":10,"email":"123@qq.com"},{"gender":"女","name":"李四","age":24,"email":"456@qq.com"},{"gender":"男","name":"王五","age":24,"email":"789@qq.com"}]
jsonStr:[{"gender":"男","name":"张三","age":10,"email":"123@qq.com"},{"gender":"女","name":"李四","age":24,"email":"456@qq.com"},{"gender":"男","name":"王五","age":24,"email":"789@qq.com"}]