<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
jar包下载:https://pan.baidu.com/s/1iZiXw55TPwIxYFQQCaR9Gw
JSONObject创建JSONObject,添加属性
//创建JSONObject
JSONObject json = new JSONObject();
//添加属性
json.put("username", "张三");
json.put("password", "123");
//打印
System.out.println(json);
//增加属性
json.element("sex", "男");
json.put("age", 18);
System.out.println(json);
根据key返回输出
System.out.println(json.get("sex"));
判断输出对象的类型
boolean isArray = json.isArray();
boolean isEmpty = json.isEmpty();
boolean isNullObject = json.isNullObject();
System.out.println("是否数组:"+isArray+", 是否空:"+isEmpty+", 是否空为空对象:"+isNullObject);
把JSONArray添加到JSONObject中
/把JSONArray添加到JSONObject中
JSONArray jsonArray = new JSONArray();
jsonArray.add(0, "张三");
jsonArray.add(1, "123");
//开始添加
json.element("student", jsonArray);
System.out.println(json);
全部代码:
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class Json {
public static void main(String[] args) {
//创建JSONObject
JSONObject json = new JSONObject();
//添加属性
json.put("username", "张三");
json.put("password", "123");
//打印
System.out.println(json);
//增加属性
json.element("sex", "男");
json.put("age", 18);
System.out.println(json);
//根据key返回
System.out.println(json.get("sex"));
//判断输出对象的类型
boolean isArray = json.isArray();
boolean isEmpty = json.isEmpty();
boolean isNullObject = json.isNullObject();
System.out.println("是否数组:"+isArray+", 是否空:"+isEmpty+", 是否空为空对象:"+isNullObject);
System.out.println("=====");
//把JSONArray添加到JSONObject中
JSONArray jsonArray = new JSONArray();
jsonArray.add(0, "张三");
jsonArray.add(1, "123");
//开始添加
json.element("student", jsonArray);
System.out.println(json);
}
}
运行结果:
JSONArray创建JSONArray,添加属性值
//创建JSONArray
JSONArray jsonArray = new JSONArray();
//添加
jsonArray.add(0, "张三");
jsonArray.add(1, "123");
jsonArray.element("男");
System.
根据下标返回输出
System.out.println(jsonArray.get(0));
根据下标设置新值,修改
jsonArray.set(0, "李四");
System.out.println(jsonArray);
//把JSONObject放入到JSONArray中
JSONObject jsonObject = new JSONObject();
jsonObject.put("username", "张三");
jsonObject.put("password", "123");
jsonArray.add(jsonObject);
System.
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class Json {
public static void main(String[] args) {
//创建JSONArray
JSONArray jsonArray = new JSONArray();
//添加
jsonArray.add(0, "张三");
jsonArray.add(1, "123");
jsonArray.element("男");
System.out.println(jsonArray);
//根据下标返回输出
System.out.println(jsonArray.get(0));
//根据下标设置新值,修改
jsonArray.set(0, "李四");
System.out.println(jsonArray);
//把JSONObject放入到JSONArray中
JSONObject jsonObject = new JSONObject();
jsonObject.put("username", "张三");
jsonObject.put("password", "123");
jsonArray.add(jsonObject);
System.out.println(jsonArray);
//循环输出
for(int i = 0; i < jsonArray.size(); i++) {
System.out.println(jsonArray.get(i));
}
}
}
运行结果
JavaBean与json字符串互转student类:
public class Student {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Student(String username, String password) {
super();
this.username = username;
this.password = password;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "Student [username=" + username + ", password=" + password + "]";
}
}
定义对象,JavaBean对象转json字符串
//定义对象
Student stu = new Student("张三", "123456");
//JavaBean对象转json字符串
JSONObject jsonObject = JSONObject.fromObject(stu);
System.out.println(jsonObject);
json字符串转为javaBean
//json字符串转为javaBean
//定义json字符串
String jsondata = "{\"username\":\"李四\", \"password\":\"123\"}";
//转为json对象
JSONObject json = JSONObject.fromObject(jsondata);
//转为JavaBean对象
Student stu2 = (Student)JSONObject.toBean(json, Student.class);
System.out.println(stu2.toString());
全部代码:
import net.sf.json.JSONObject;
public class Json {
public static void main(String[] args) {
//定义对象
Student stu = new Student("张三", "123456");
//JavaBean对象转json字符串
JSONObject jsonObject = JSONObject.fromObject(stu);
System.out.println(jsonObject);
//json字符串转为javaBean
//定义json字符串
String jsondata = "{\"username\":\"李四\", \"password\":\"123\"}";
//转为json对象
JSONObject json = JSONObject.fromObject(jsondata);
//转为JavaBean对象
Student stu2 = (Student)JSONObject.toBean(json, Student.class);
System.out.println(stu2.toString());
}
}
输出结果:
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |