| 类型 | 描述 |
|---|---|
| Number | 数字型 |
| String | 字符串型 |
| Boolean | 布尔型 |
| Array | 数组 |
| Object | 对象 |
| null | 空值 |
JSONObject obj = new JSONObject(); obj.put(key, value);
import org.json.JSONObject;
public class JSONObjectSample {
public static void main(String[] args) {
createJson();
}
private static void createJson() {
JSONObject obj = new JSONObject();
obj.put("name", "John");
obj.put("sex", "male");
obj.put("age", 22);
obj.put("is_student", true);
obj.put("hobbies", new String[] {"hiking", "swimming"});
//调用toString()方法可直接将其内容打印出来
System.out.println(obj.toString());
}
}
{"hobbies":["hiking","swimming"],"sex":"male","name":"John","is_student":true,"age":22}
public class JSONObjectSample {
public static void main(String[] args) {
createJsonByMap();
}
private static void createJsonByMap() {
Map<String, Object> data = new HashMap<String, Object>();
data.put("name", "John");
data.put("sex", "male");
data.put("age", 22);
data.put("is_student", true);
data.put("hobbies", new String[] {"hiking", "swimming"});
JSONObject obj = new JSONObject(data);
System.out.println(obj.toString());
}
}
public class PersonInfo {
private String name;
private String sex;
private int age;
private boolean isStudent;
private String[] hobbies;
public void setName(String name) {
this.name = name;
}
public void setSex(String sex) {
this.sex = sex;
}
public void setAge(int age) {
this.age = age;
}
public void setStudent(boolean isStudent) {
this.isStudent = isStudent;
}
public void setHobbies(String[] hobbies) {
this.hobbies = hobbies;
}
//getter不能少
public String getName() {
return name;
}
public String getSex() {
return sex;
}
public int getAge() {
return age;
}
public boolean isStudent() {
return isStudent;
}
public String[] getHobbies() {
return hobbies;
}
}
import org.json.JSONObject;
public class JSONObjectSample {
public static void main(String[] args) {
createJsonByJavaBean();
}
private static void createJsonByJavaBean() {
PersonInfo info = new PersonInfo();
info.setName("John");
info.setSex("male");
info.setAge(22);
info.setStudent(true);
info.setHobbies(new String[] {"hiking", "swimming"});
JSONObject obj = new JSONObject(info);
System.out.println(obj);
}
}
{
"hobbies": [
"hiking",
"swimming"
],
"sex": "male",
"name": "John",
"is_student": true,
"age": 22
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.studying</groupId> <artifactId>myjson</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>myjson</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20160810</version> </dependency> <!--加入对commons-io的依赖--> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> </dependencies> </project>
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.json.JSONArray;
import org.json.JSONObject;
public class JSONObjectSample {
public static void main(String[] args) throws IOException {
File file = new File("src/main/java/demo.json");
String content = FileUtils.readFileToString(file);
//对基本类型的解析
JSONObject obj = new JSONObject(content);
System.out.println("name:" + obj.getString("name"));
System.out.println("sex:" + obj.getString("sex"));
System.out.println("age" + obj.getInt("age"));
System.out.println("is_student" + obj.getBoolean("is_student"));
//对数组的解析
JSONArray hobbies = obj.getJSONArray("hobbies");
System.out.println("hobbies:");
for (int i = 0; i < hobbies.length(); i++) {
String s = (String) hobbies.get(i);
System.out.println(s);
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有