JSONObject 使用方法小结
package com.sun.zuul.jsonObject;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.*;
public class JsonObjectTest {
/**
* put() 方法的第一个参数为 key 值,必须为 String 类型,第二个参数为 value,可以为 boolean、double、int、long、Object、Map 以及 Collection 等
* <p>
* double int 等数值类型写到json 时都以Number 类型存储
*
* @param args
*/
public static void main(String[] args) {
// beanToJson();
// testJson2();
testJsonO2OString();
}
// 解码
public static void testJson2() {
// 从字符串解析json对象
JSONObject object = JSONObject.parseObject("{\"boolean\":true,\"string\":\"string\",\"list\":[1,2,3],\"int\":2}");
String s = object.getString("string");
System.out.println(s);
int i = object.getIntValue("i");
System.out.println(i);
boolean b = object.getBoolean("boolean");
System.out.println(b);
// JSON.parseArray从字符串解析Json 数组,
// JSON.toJsonString 将json 对象或者json数组转化为字符串
List<Integer> list = JSON.parseArray(object.getJSONArray("list").toJSONString(), Integer.class);
list.forEach(System.out::println);
System.out.println(object.getString("null"));
/**
* 结果如下
* string
* 0
* true
* 1
* 2
* 3
* null
*/
}
/**
* JsonObject.parseObject 从字符串解析json对象
* JSON.parseArray从字符串解析Json 数组,
* JSON.toJsonString 将json 对象或者json数组转化为字符串
*
*/
public static void testJsonO2OString() {
//从字符串解析JSON对象
JSONObject obj = JSON.parseObject("{\"runoob\":\"菜鸟教程\"}");
//从字符串解析JSON数组
JSONArray arr = JSON.parseArray("[\"菜鸟教程\",\"RUNOOB\"]\n");
//将JSON对象转化为字符串
String objStr = JSON.toJSONString(obj);
//将JSON数组转化为字符串
String arrStr = JSON.toJSONString(arr);
System.out.println(objStr);
System.out.println(arrStr);
}
// map 转jsonObject, 编码
public static void mapTOjson() {
Map<String, Object> map = new HashMap<>();
map.put("name1", "xiaoliu");
map.put("sex1", "fale");
map.put("age", 28);
map.put("address", "北京");
JSONObject json = new JSONObject(map);
System.out.println(json);
}
// 直接构建,编码
public static void createJsonObject() {
JSONObject object = new JSONObject();
object.put("name", "ms");
object.put("sex", "fale");
object.put("age", 2);
String[] ss = new String[]{"aa", "bb", "cc"};
object.put("ss", ss);
System.out.println(object);
}
public static void beanToJson() {
Person person = new Person();
person.setName("xiaoming");
person.setAddress("beijing");
person.setAge(22);
person.setSex("nan");
person.setFriends(new String[]{"xiaohong", "xiaohong"});
// 序列化
String text = JSONObject.toJSONString(person);
// 反序列化
Person p = JSONObject.parseObject(text, Person.class);
System.out.println(p);
// 这样是错误的 com.sun.zuul.jsonObject.Person cannot be cast to java.util.Map
// JSONObject data = new JSONObject((Map<String, Object>) person);
// JSONObject data = (JSONObject) JSONObject.toJSON(person);
//
// System.out.println(data);
//
// // 解析json
// data = data.getJSONObject("person");
// System.out.println(data);
// String name = data.getString("name");
// String address = data.getString("address");
// String[] friends = data.getJSONArray("friends").toArray(new String[0]);
// System.out.println(name + " " + address + " " + friends);
}
public List<Person> getPersonLists(JSONObject data) {
List<Person> list = new ArrayList<>();
return list;
}
}
class Person {
private String name;
private String sex;
private int age;
private String address;
private String[] friends;
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
", address='" + address + '\'' +
", friends=" + Arrays.toString(friends) +
'}';
}
public String[] getFriends() {
return friends;
}
public void setFriends(String[] friends) {
this.friends = friends;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}