import com.alibaba.fastjson.JSONObject;
public class JSONTest {
// 创建JSONObject对象
private static JSONObject createJSONObject() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", "张三");
jsonObject.put("age", new Integer(26));
JSONObject innerObj = new JSONObject();
innerObj.put("university", "某某学校");
innerObj.put("special", "某某专业");
jsonObject.put("education", innerObj);
return jsonObject;
}
public static void main(String[] args) {
JSONObject jsonObject = new JSONTest().createJSONObject();
// 输出对象
System.out.println("jsonObject:" + jsonObject);
// 根据key返回一个字符串
String name = jsonObject.getString("name");
System.out.println(name);
// 输出组合对象中的属性
System.out.println(jsonObject.getJSONObject("education"));
}
}
public class JSONTest {
// 创建JSONObject对象
private static JSONObject createJSONObject() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", "张三");
jsonObject.put("age", new Integer(26));
JSONObject innerObj = new JSONObject();
innerObj.put("university", "某某学校");
innerObj.put("special", "某某专业");
jsonObject.put("education", innerObj);
return jsonObject;
}
public static void main(String[] args) {
JSONObject jsonObject = new JSONTest().createJSONObject();
// 输出对象
System.out.println("jsonObject:" + jsonObject);
// 根据key返回一个字符串
String name = jsonObject.getString("name");
System.out.println(name);
// 输出组合对象中的属性
System.out.println(jsonObject.getJSONObject("education"));
}
}