JSONObject和JSONArray的简单使用(json-lib)
一. jar包
- commons-lang.jar
- commons-beanutils.jar
- commons-collections.jar
- commons-logging.jar
- ezmorph.jar
- json-lib-2.2.2-jdk15.jar
二. 代码
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import net.sf.json.JSONArray;
import net.sf.json.JSONObject; public class Test { public static void main(String[] args) {
new Test().test1();
new Test().test2();
new Test().test3();
new Test().test4();
new Test().test5();
new Test().test6();
new Test().test7();
} /**
* JSON字符串转JSONObject对象
*/
public void test1() {
String jsonStr = "{\"name\":\"ZhangSan\",\"sex\":\"boy\",\"age\":18}";
JSONObject jsonObj = JSONObject.fromObject(jsonStr);
System.out.println(jsonObj.toString());
} /**
* 简单JSONObject对象转java对象
*/
public void test2() {
JSONObject jsonObj = new JSONObject();
jsonObj.put("name", "LiSi");
jsonObj.put("sex", "girl");
jsonObj.put("age", 17);
Student student = (Student) JSONObject.toBean(jsonObj, Student.class);
System.out.println(student.getName() + " | " + student.getSex() + " | " + student.getAge());
} /**
* 复杂JSONObject对象转java对象
*/
@SuppressWarnings("rawtypes")
public void test3() {
List<Student> students = new ArrayList<Student>();
students.add(new Student("ZhangSan", "boy", 18));
students.add(new Student("LiSi", "girl", 17)); BanJi banji = new BanJi();
banji.setBanJiName("日语二班");
banji.setStudents(students); JSONObject jsonObj = JSONObject.fromObject(banji);
Map<String, Class> classMap = new HashMap<String, Class>();
classMap.put("students", Student.class);
BanJi banji2 = (BanJi) JSONObject.toBean(jsonObj, BanJi.class, classMap);
System.out.println(banji2.getStudents().get(0).getName()); // 验证转换是否成功
} /**
* 简单java集合对象转JSONArray
*/
public void test4() {
List<Student> students = new ArrayList<Student>();
students.add(new Student("ZhangSan", "boy", 18));
students.add(new Student("LiSi", "girl", 17));
JSONArray jsonArray = JSONArray.fromObject(students);
System.out.println(jsonArray.toString());
} /**
* JSONArray转java集合对象
*/
@SuppressWarnings({ "unchecked", "deprecation" })
public void test5() {
JSONObject jsonObj1 = new JSONObject();
jsonObj1.put("name", "ZhangSan");
jsonObj1.put("sex", "boy");
jsonObj1.put("age", 18); JSONObject jsonObj2 = new JSONObject();
jsonObj2.put("name", "lisi");
jsonObj2.put("sex", "girl");
jsonObj2.put("age", 17); JSONArray jsonArray = new JSONArray();
jsonArray.add(0, jsonObj1);
jsonArray.add(1, jsonObj2); List<Student> students3 = JSONArray.toList(jsonArray, Student.class);
System.out.println(students3.get(0).getName());
System.out.println(students3.get(1).getName());
} /**
* 复杂java集合对象转JSONArray
*/
@SuppressWarnings({ "rawtypes", "unchecked", "deprecation" })
public void test6() {
BanJi banji1 = new BanJi();
banji1.setBanJiName("日语一班");
List<Student> students1 = new ArrayList<Student>();
students1.add(new Student("ZhangSan", "boy", 18));
banji1.setStudents(students1); BanJi banji2 = new BanJi();
banji2.setBanJiName("日语二班");
List<Student> students2 = new ArrayList<Student>();
students1.add(new Student("LiSi", "girl", 17));
banji2.setStudents(students2); List<BanJi> banjiList = new ArrayList<BanJi>();
banjiList.add(banji1);
banjiList.add(banji2); JSONArray jsonArray = JSONArray.fromObject(banjiList);
Map<String, Class> classMap = new HashMap<String, Class>();
classMap.put("students", Student.class);
List<BanJi> banjiList2 = JSONArray.toList(jsonArray, BanJi.class, classMap);
System.out.println(banjiList2.get(0).getStudents().get(0).getName()); } /**
* 拆分JSONArray为JSONObject
*/
public void test7() {
JSONObject jsonObj1 = new JSONObject();
jsonObj1.put("name", "ZhangSan");
jsonObj1.put("sex", "boy");
jsonObj1.put("age", 18); JSONObject jsonObj2 = new JSONObject();
jsonObj2.put("name", "lisi");
jsonObj2.put("sex", "girl");
jsonObj2.put("age", 17); JSONArray jsonArray = new JSONArray();
jsonArray.add(0, jsonObj1);
jsonArray.add(1, jsonObj2); JSONObject object = (JSONObject) jsonArray.get(0);
System.out.println(object.toString());
} }
import java.util.List; /**
* 班级类
*/
public class BanJi { private String banJiName; // 班级名 private List<Student> students; // 学生 /**
* 构造函数
*/
public BanJi() {
super();
} // getters/setters(略)
}
/**
* 学生类
*/
public class Student { private String name; // 姓名 private String sex; // 性别 private int age; // 年龄 /**
* 构造函数
*/
public Student() {
super();
} /**
* 构造函数
*/
public Student(String name, String sex, int age) {
super();
this.name = name;
this.sex = sex;
this.age = age;
} // getters/setters(略)
}
JSONObject和JSONArray的简单使用(json-lib)的更多相关文章
- 嵌套的JsonObject与JSONArray的取值---JSON中嵌套JSONArray
在复杂的JSON数据的格式中,往往会对JSON数据进行嵌套,这样取值会比之前的取值稍微复杂一点,但是只要思路清晰,其实取法还是一样的.就跟if else语句一样,如果if中套if,if中再套if,写的 ...
- Java之JSON处理(JSONObject、JSONArray)
依赖包:json-20180130.jar MAVEN地址: <dependency> <groupId>org.json</groupId> <artifa ...
- net.sf.json JSONObject与JSONArray使用实例
实例自己想的一个实例应用场景:一个人可以有多个角色,例如:在家中是儿子,在学校是学生,在公司是程序员,一个人还可以办好多业务 * 每个业务好多个人都可以办,则标记(mark)就是记录这唯一标识的(如i ...
- json学习系列(6)JSONObject和JSONArray是JDK的集合部分延伸
我一直觉得JSONObject和JSONArray是JDK集合部分的延伸,它们与JDK的List和Map一脉相承.通过研究JSONObject和JSONArray的结构,我们顺便也复习一下JDK的内容 ...
- json:JSONObject与JSONArray的使用
1.JAR包简介 要使程序可以运行必须引入JSON-lib包,JSON-lib包同时依赖于以下的JAR包: commons-lang.jar commons-beanutils.jar commons ...
- Fastjson, Gson, org.json.JSON三者对于JSONObject及JSONArray的判断
1.Fastjson 我们通常在已知格式的情况下直接使用JSONObject,JSONArray,但是如果遇到需要判断格式呢? try{ Object object = JSON.parse(a); ...
- json字符串转JSONObject和JSONArray以及取值
import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class JsonTest { public static v ...
- [转]java中JSONObject与JSONArray的使用详细说明及有关JSON的工具类
JSONObject与JSONArray的使用 一.JAR包简介 要使程序可以运行必须引入JSON-lib包,JSON-lib包同时依赖于以下的JAR包: 1.commons-lang.jar 2.c ...
- 011-JSON、JSONObject、JSONArray使用、JSON数组形式字符串转换为List<Map<String,String>>的8种方法
一.JSON数据格式 1.1.常用JSON数据格式 1.对象方式:JSONObject的数据是用 { } 来表示的, 例如: { "id" : "123", & ...
随机推荐
- pyqt QTableWidgetItem多行显示
def __2(self): t1=QtGui.QTableWidgetItem(self.names.text()) self.tabs.tableinsertinto.setItem(0,0,t1 ...
- proxy 利用get拦截,实现一个生成各种DOM节点的通用函数dom。
const dom = new Proxy({}, { get(target, property) { return function(attrs = {}, ...children) { const ...
- Android软件开发之常用系统控件界面整理
1.文本框TextView TextView的作用是用来显示一个文本框,下面我用两种方式为大家呈现TextView, 第一种是通过xml布局文件呈现 ,第二种是通过代码来呈现,由此可见Android ...
- 微信小程序demo豆瓣图书
最近微信小程序被炒得很火热,本人也抱着试一试的态度下载了微信web开发者工具,开发工具比较简洁,功能相对比较少,个性化设置也没有.了解完开发工具之后,顺便看了一下小程序的官方开发文档,大概了解了小程序 ...
- input输入字母自动大小写转换
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- git学习笔记(五)
---恢复内容开始--- 嗯 今天又看到一个非常不错的入门git教程 Mark一下 阮一峰Git操作详解 主要讲解了五个常用的git命令 git clone git remote git fetch ...
- C# Datatable导出Excel方法
C# 导出Excel方法 先引用下System.IO;System.data; 具体函数如下: public static bool ExportCSV(DataTable dt, string f ...
- 是什么让我想到开发NFinal
我是从01前开始就接触.net,那时.net还是1.0时代,很多东西都没有.后来.net出了2.0版本.从vs2005开始就使用Webform做网站.当时感觉.net能够拖来拖去,很厉害.参加工作后, ...
- jquery怎么获取URL的参数
function request(paras) { var url = location.href; var paraString = ur ...
- 解不定方程ax+by=m的最小解
给出方程a*x+b*y=c,其中所有数均是整数,且a,b,c是已知数,求满足那个等式的x,y值?这个方程可能有解也可能没解也可能有无穷多个解(注意:这里说的解都是整数解)? 既然如此,那我们就得找出有 ...