- Fastjson 是一个 Java 库,可以将 Java 对象转换为 JSON 格式,当然它也可以将 JSON 字符串转换为 Java 对象。
- Fastjson 可以操作任何 Java 对象,即使是一些预先存在的没有源码的对象。
- 提供了 toJSONString() 和 parseObject() 方法来将 Java 对象与 JSON 相互转换。调用toJSONString方 法即可将对象转换成 JSON 字符串,parseObject 方法则反过来将 JSON
字符串转换成对象。- 允许转换预先存在的无法修改的对象(只有class、无源代码)。
- Java泛型的广泛支持。
- 允许对象的自定义表示、允许自定义序列化类。
- 支持任意复杂对象(具有深厚的继承层次和广泛使用的泛型类型)。
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>1.2.83version>
dependency>
JSONObject相当于Map
String jsonText = "{\n" +
"\t\"field1\":\"1\",\n" +
"\t\"field2\":\"2\"\n" +
"}";
JSONObject jsonObject = JSONObject.parseObject(jsonText);
System.out.println(jsonObject.get("field1"));
JSONArray 相当于 List < Object >,例如:
String jsonText = "[{\"field1\":\"1\",\"field2\":\"2\"},{\"field1\":\"3\",\"field2\":\"3\"}]";
JSONArray jsonArray = JSONObject.parseArray(jsonText);
for (Object o : jsonArray) {
System.out.println(o);
}
SerializeWriter 相当于 StringBuffer,例如:
SerializeWriter serializeWriter = new SerializeWriter();
serializeWriter.append("[");
serializeWriter.append("{");
serializeWriter.append("'field1':'1','field2':2");
serializeWriter.append("}");
serializeWriter.append("]");
String jsonText = serializeWriter.toString();
System.out.println(jsonText);
创建一个内部类用来测试
static class TestObject{
private String field1;
private String field2;
public String getField1() {
return field1;
}
public void setField1(String field1) {
this.field1 = field1;
}
public String getField2() {
return field2;
}
public void setField2(String field2) {
this.field2 = field2;
}
public TestObject() {
}
public TestObject(String field1, String field2) {
this.field1 = field1;
this.field2 = field2;
}
@Override
public String toString() {
return "TestObject{" +
"field1='" + field1 + '\'' +
", field2='" + field2 + '\'' +
'}';
}
}
TestObject testObject = new TestObject("1","2");
String jsonString = JSONObject.toJSONString(testObject, true);
System.out.println(jsonString);
List<TestObject> list = Arrays.asList(new TestObject("1", "2"), new TestObject("3", "3"));
String jsonString = JSONObject.toJSONString(list, false);
System.out.println(jsonString);
String jsonText = "{\n" +
"\t\"field1\":\"1\",\n" +
"\t\"field2\":\"2\"\n" +
"}";
TestObject testObject = JSONObject.parseObject(jsonText, TestObject.class);
System.out.println(testObject);
String jsonText = "{\n" +
"\t\"field1\":\"1\",\n" +
"\t\"field2\":\"2\"\n" +
"}";
JSONObject jsonObject = JSONObject.parseObject(jsonText);
String field1 = String.valueOf(jsonObject.get("field1"));
System.out.println(field1);
String jsonText = "[{\"field1\":\"1\",\"field2\":\"2\"},{\"field1\":\"3\",\"field2\":\"3\"}]";
List<TestObject> arrayLists = JSONObject.parseArray(jsonText, TestObject.class);
for (TestObject testObject : arrayLists) {
System.out.println(testObject);
}
String jsonText = "{\n" +
"\t\"fieldName1\":\"1\",\n" +
"\t\"fieldName2\":\"2\"\n" +
"}";
Map<String,Object> map = JSONObject.parseObject(jsonText, HashMap.class);
System.out.println(map);
String jsonText = "[1,2,3,1]";
Set<String> set = JSONObject.parseObject(jsonText, HashSet.class);
System.out.println(set);
String jsonText = "{\n" +
"\t\"fieldName1\":\"1\",\n" +
"\t\"fieldName2\":\"2\"\n" +
"}";
JSONObject jsonObject = JSONObject.parseObject(jsonText);
Set<String> keys = jsonObject.keySet();
keys.remove("fieldName1");
System.out.println(jsonObject);
String jsonText = "{\n" +
"\t\"fieldName1\":\"1\",\n" +
"\t\"fieldName2\":\"2\"\n" +
"}";
JSONObject jsonObject = JSONObject.parseObject(jsonText);
jsonObject.put("fieldName3","3");
System.out.println(jsonObject);
String jsonText = "{\n" +
"\t\"fieldName1\":\"1\",\n" +
"\t\"fieldName2\":\"2\"\n" +
"}";
JSONObject jsonObject = JSONObject.parseObject(jsonText);
jsonObject.put("fieldName2","22");
System.out.println(jsonObject);
String jsonText = "{\n" +
"\t\"fieldName1\":\"1\",\n" +
"\t\"fieldName2\":\"2\"\n" +
"}";
JSONObject jsonObject = JSONObject.parseObject(jsonText);
boolean result = jsonObject.containsKey("fieldName1");
System.out.println(result);
Date date = new Date();
String jsonStringWithDateFormat = JSON.toJSONStringWithDateFormat(date, "yyyy-MM-dd HH:mm:ss");
System.out.println(jsonStringWithDateFormat);