1 @JsonProperty("jsonName") 2 private String name;
2 private String name;
FastJson主要用到了如下三个类:
JSON:FastJson的解析器,用于JSON格式字符串、JSON对象及javaBean之间的转换;html
Json对象JSONObject;java
Json数组对象JSONArray。json
JSONObject和JSONArray继承了JSON,能够直接用于转换。首先,JSONObject本质上能够看做一个Map<String,Object>数组
1 public class JSONObject extends JSON implements Map<String, Object>, Cloneable, Serializable, InvocationHandler {
而后,JSONArray本质上能够看做是一个List<Object>dom
1 public class JSONArray extends JSON implements List<Object>, Cloneable, RandomAccess, Serializable {
FastJson的一些使用以下所示:网站
Json串转换成JSONObjectthis
1 {"header":{"code":0,"message":"SUCCESS"},"data":{"id":48,"metric.name":"test_hk_1","metric.type":"kafka_commit","partition.lag":false,"group.id":""}}
1 import com.alibaba.fastjson.JSONObject; 2 JSONObject jsonObject = JSONObject.parseObject(contentAsString).getJSONObject("data");
1 String str = JSONObject.toJSONString(jsonObject);
原始字符串spa
1 private static final String JSON_ARRAY_STR = "[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]";
转换过程code
1 import com.alibaba.fastjson.JSONArray; 2 JSONArray jsonArray = JSONArray.parseArray(JSON_ARRAY_STR); 3 4 for (Object obj : jsonArray) { 5 JSONObject jsonObject = (JSONObject) obj; 6 } 7 }
原始字符串同上例,JavaBean以下所示htm
1 public class Student { 2 3 private String studentName; 4 private Integer studentAge; 5 6 public String getStudentName() { 7 return studentName; 8 } 9 10 public void setStudentName(String studentName) { 11 this.studentName = studentName; 12 } 13 14 public Integer getStudentAge() { 15 return studentAge; 16 } 17 18 public void setStudentAge(Integer studentAge) { 19 this.studentAge = studentAge; 20 } 21 }
转换过程
1 Student student = JSONObject.parseObject(JSON_OBJ_STR, new TypeReference<Student>() {});
或者
1 Student student = JSONObject.parseObject(JSON_OBJ_STR, Student.class);
1 String res = JSON.toJSONString(student);
⚠️注意⚠️:若是这里student对象没有getter方法,没法转换成Json字符串的。另外,在转换Json字符串的过程当中,能够对特殊字段进行设置,好比将对象中属性的null值输出:
1 String str = JSONObject.toJSONString(student, SerializerFeature.WriteMapNullValue);
QuoteFieldNames | 输出key时是否使用双引号,默认为true |
WriteMapNullValue | 输出值为null的字段,是否默认为false |
WriteNullNumberAsZero | 数值字段若是为null,输出为0,而非null |
WriteNullListAsEmpty | List字段若是为null,输出为[],而非null |
WriteNullStringAsEmpty | 字符类型字段若是为null,输出为"",而非null |
WriteNullBooleanAsFalse | Boolean字段若是为null,输出为false,而非null |
1 JSONObject jsonObject = (JSONObject)JSON.toJSON(student);
浅拷贝
1 JSONObject b = new JSONObject(a);
深拷贝
1 JSONObject b = (JSONObject)a.clone();
上边的深拷贝很差使的话,能够考虑下边这个
1 private Object deepCopyByJson(Object obj) { 2 String json = JSON.toJSONString(obj); 3 return JSON.parseObject(json, Object.class); 4 }
1 "{\"header\":{\"code\":34,\"message\":\"job id doesn't exist.\"},\"data\":null}"
boolean,int等类型的value以下所示:
1 "{\"header\":{\"code\":0,\"message\":\"SUCCESS\"},\"data\":{\"id\":48,\"metric.type\":\"kafka_commit\",\"consumer.zk.host\":\"\",\"consumer.zk.port\":2181,\"consumer.zk.root\":\"/brokers\",\"enabled\":true,\"group.id\":\"\"}}"