学习FastJson和Gson的用法

111 阅读2分钟
1.用FastJson,将JSON字符串转换为FastJson对象:
import com.alibaba.fastjson.JSONObject;  
String jsonString = "{\"name\":\"John\", \"age\":30, \"city\":\"New York\"}";  
JSONObject jsonObject = JSONObject.parseObject(jsonString);
2.用FastJson,将JSON字符串转换为Java对象:
import com.alibaba.fastjson.JSONObject;  
String jsonString = "{\"name\":\"John\", \"age\":30, \"city\":\"New York\"}";  
Person person = JSONObject.parseObject(jsonString, Person.class);
4.将JSON字符串转换为Java的集合对象:
import com.alibaba.fastjson.JSONArray;  
String jsonString = "[{\"name\":\"John\", \"age\":30, \"city\":\"New York\"}, {\"name\":\"Mary\", \"age\":25, \"city\":\"Los Angeles\"}]";  
List<Person> personArray = JSONArray.parseArray(jsonString, Person.class);
5.将JSON字符串转换为Java的集合对象:
import com.alibaba.fastjson.JSON;  
List<Person> personList = new ArrayList<Person>();  
Person person1 = new Person();  
person1.setName("John");  
person1.setAge(30);  
person1.setCity("New York");  
Person person2 = new Person();  
person2.setName("Mary");  
person2.setAge(25);  
person2.setCity("Los Angeles");  
personList.add(person1);  
personList.add(person2);  
String jsonString = JSON.toJSONString(personList);
6.用Gson,添加Json对象:
Gson gson = new Gson();  
JsonObject jsonObject1 = new JsonObject();  
jsonObject1.addProperty("name", "John");  
jsonObject1.addProperty("age", 30);  
JsonObject jsonObject2 = new JsonObject();  
jsonObject2.addProperty("city", "New York");  
jsonObject2.addProperty("country", "USA");  
jsonObject1.add("location", jsonObject2); // 将jsonObject2作为一个新的属性添加到jsonO
7.用Gson,添加Json数组:
Gson gson = new Gson();  
JsonArray jsonArray = new JsonArray();  
jsonArray.add("Java");  
jsonArray.add("Python");  
jsonArray.add("C++");  
JsonObject jsonObject = new JsonObject();  
jsonObject.add("languages", jsonArray); // 将jsonArray作为一个新的属性添加到jsonObject中
8.用Gson,移除值:
son gson = new Gson();  
JsonObject jsonObject = new JsonObject();  
jsonObject.addProperty("name", "John");  
jsonObject.addProperty("age", 30);  
jsonObject.removeProperty("age"); // 移除age属性
9.用Gson,移除Json对象:
Gson gson = new Gson();  
JsonObject jsonObject1 = new JsonObject();  
jsonObject1.addProperty("name", "John");  
jsonObject1.addProperty("age", 30);  
JsonObject jsonObject2 = new JsonObject();  
jsonObject2.addProperty("city", "New York");  
jsonObject2.addProperty("country", "USA");  
jsonObject1.add("location", jsonObject2);  
jsonObject1.remove("location"); // 移除location属性,包括其包含的jsonObject2对象
10.用Gson,将JSON字符串转换为Gson对象:
Gson gson = new Gson();  
String jsonString = "{\"name\":\"John\", \"age\":30, \"city\":\"New York\"}";  
JsonObject jsonObject = gson.fromJson(jsonString, JsonObject.class);
11.用Gson,json字符串转JAVA对象:
Gson gson = new Gson();  
String jsonString = "{\"name\":\"John\", \"age\":30, \"city\":\"New York\"}";  
Person person = gson.fromJson(jsonString, Person.class);
12.用Gson,JSON字符串转换为Java的集合对象:
Gson gson = new Gson();  
String jsonString = "[{\"name\":\"John\", \"age\":30, \"city\":\"New York\"}, {\"name\":\"Mary\", \"age\":25, \"city\":\"Los Angeles\"}]";  
Person[] personArray = gson.fromJson(jsonString, Person[].class);  
List<Person> personList = gson.fromJson(jsonString, new TypeToken<List<Person>>() {}.getType());
13.用Gson,将Gson对象转换为JSON字符串:
Gson gson = new Gson();  
JsonObject jsonObject = new JsonObject();  
jsonObject.addProperty("name", "John");  
jsonObject.addProperty("age", 30);  
jsonObject.addProperty("city", "New York");  
String jsonString = gson.toJson(jsonObject);
14.用Gson,将Java对象转换为JSON字符串:
Gson gson = new Gson();  
Person person = new Person();  
person.setName("John");  
person.setAge(30);  
person.setCity("New York");  
String jsonString = gson.toJson(person);
15.用Gson,将Java的集合对象转换为JSON字符串:
Gson gson = new Gson();  
List<Person> personList = new ArrayList<Person>();  
Person person1 = new Person();  
person1.setName("John");  
person1.setAge(30);  
person1.setCity("New York");  
Person person2 = new Person();  
person2.setName("Mary");  
person2.setAge(25);  
person2.setCity("Los Angeles");  
personList.add(person1);  
personList.add(person2);  
String jsonString = gson.toJson(personList);