目录
1. 基本说明
2. 引入依赖
3. 其他类型转Json对象
3.1 json字符串转JsonOject
3.2 json数组字符串转JsonArray
3.3 JavaBean 转 JsonObject
3.4 JavaBean List转JsonArray
4. 其他类型转Json字符串
4.1 将Map转Json字符串
4.2 将Java Bean 转Json字符串
4.3 将JavaBean List转Json字符串
FastJson 与 Google 的 Gson 都是解析 Json 的强者
github地址: GitHub - alibaba/fastjson: FASTJSON 2.0.x has been released, faster and more secure, recommend you upgrade.
com.alibaba
fastjson
1.2.49
/**
* json字符串转JsonOject
*/
public static void test1() {
String jsonStr = "{\"name\":\"sanqian\",\"age\":10,\"address\": \"shen zhen\"}";
JSONObject jsonObject = JSONObject.parseObject(jsonStr);
System.out.println(jsonObject.getString("name"));//输出one
System.out.println(jsonObject.getInteger("age"));//输出110
System.out.println(jsonObject.getString("address"));//输出null
}
/**
* json数组字符串转JsonArray
*/
public static void test2(){
String jsonArrStr = "[{\"name\":\"张三\",\"age\":25},{\"name\":\"李四\",\"age\":28}]";
JSONArray jsonArray = JSONObject.parseArray(jsonArrStr);
for(Object object: jsonArray){
JSONObject jsonObject = (JSONObject) object;
System.out.println(jsonObject.get("name"));
System.out.println(jsonObject.get("age"));
System.out.println("--------------------");
}
}
/**
* JavaBean 转 JsonObject
*/
public static void test3(){
Person person1 = new Person();
person1.setName("张三");
person1.setAge(28);
person1.setBirthday(new Date());
// 方式一:
JSONObject jsonObject = (JSONObject) JSONObject.toJSON(person1);
System.out.println(jsonObject);
System.out.println(jsonObject.get("name"));
//方式二
String jsonString = JSONObject.toJSONString(person1);
JSONObject jsonObject1 = JSONObject.parseObject(jsonString);
System.out.println(jsonObject1);
}
/**
* JavaBean List转JsonArray
*/
public static void test4(){
Person person1 = new Person();
person1.setName("张三");
person1.setAge(28);
person1.setBirthday(new Date());
Person person2 = new Person();
person2.setName("李四");
person2.setAge(25);
person2.setBirthday(new Date());
List persons = new ArrayList();
persons.add(person1);
persons.add(person2);
/**方式1*/
String jsonArrStr = JSONArray.toJSONString(persons);
JSONArray jsonArray = JSONArray.parseArray(jsonArrStr);
JSONObject jsonObject1 = (JSONObject)jsonArray.get(0);
System.out.println(jsonObject1.get("name"));//输出:张三
/**方式2*/
JSONArray jsonArray1 = (JSONArray)JSONArray.toJSON(persons);
JSONObject jsonObject2 = (JSONObject)jsonArray1.get(1);
System.out.println(jsonObject2.get("name"));//输出:李四
}
/**
* 将Map转Json字符串
*/
public void test1() {
Map map = new HashMap();
map.put("key1", "One");
map.put("key2", "Two");
String mapJson = JSON.toJSONString(map);
System.out.println(mapJson);//输出:{"key1":"One","key2":"Two"}
}
/**
* 将Java Bean 转Json字符串
*/
public static void test2() {
Person person1 = new Person();
person1.setName("张三");
person1.setAge(26);
person1.setBirthday(new Date());
/**两种方式都行
* 因为JSONObject继承了JSON*/
String object = JSONObject.toJSONString(person1);
/*String object = JSON.toJSONString(person1);*/
System.out.println(object);
}
/**
* 将Java Bean List转Json字符串
*/
public static void test3() {
Person person1 = new Person();
person1.setName("张三");
person1.setAge(28);
person1.setBirthday(new Date());
Person person2 = new Person();
person2.setName("李四");
person2.setAge(25);
person2.setBirthday(new Date());
List persons = new ArrayList();
persons.add(person1);
persons.add(person2);
String object = JSON.toJSONString(persons);
System.out.println(object);
}