一. jar包

  1. commons-lang.jar
  2. commons-beanutils.jar
  3. commons-collections.jar
  4. commons-logging.jar
  5. ezmorph.jar
  6. 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)的更多相关文章

  1. 嵌套的JsonObject与JSONArray的取值---JSON中嵌套JSONArray

    在复杂的JSON数据的格式中,往往会对JSON数据进行嵌套,这样取值会比之前的取值稍微复杂一点,但是只要思路清晰,其实取法还是一样的.就跟if else语句一样,如果if中套if,if中再套if,写的 ...

  2. Java之JSON处理(JSONObject、JSONArray)

    依赖包:json-20180130.jar MAVEN地址: <dependency> <groupId>org.json</groupId> <artifa ...

  3. net.sf.json JSONObject与JSONArray使用实例

    实例自己想的一个实例应用场景:一个人可以有多个角色,例如:在家中是儿子,在学校是学生,在公司是程序员,一个人还可以办好多业务 * 每个业务好多个人都可以办,则标记(mark)就是记录这唯一标识的(如i ...

  4. json学习系列(6)JSONObject和JSONArray是JDK的集合部分延伸

    我一直觉得JSONObject和JSONArray是JDK集合部分的延伸,它们与JDK的List和Map一脉相承.通过研究JSONObject和JSONArray的结构,我们顺便也复习一下JDK的内容 ...

  5. json:JSONObject与JSONArray的使用

    1.JAR包简介 要使程序可以运行必须引入JSON-lib包,JSON-lib包同时依赖于以下的JAR包: commons-lang.jar commons-beanutils.jar commons ...

  6. Fastjson, Gson, org.json.JSON三者对于JSONObject及JSONArray的判断

    1.Fastjson 我们通常在已知格式的情况下直接使用JSONObject,JSONArray,但是如果遇到需要判断格式呢? try{ Object object = JSON.parse(a); ...

  7. json字符串转JSONObject和JSONArray以及取值

    import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class JsonTest { public static v ...

  8. [转]java中JSONObject与JSONArray的使用详细说明及有关JSON的工具类

    JSONObject与JSONArray的使用 一.JAR包简介 要使程序可以运行必须引入JSON-lib包,JSON-lib包同时依赖于以下的JAR包: 1.commons-lang.jar 2.c ...

  9. 011-JSON、JSONObject、JSONArray使用、JSON数组形式字符串转换为List<Map<String,String>>的8种方法

    一.JSON数据格式 1.1.常用JSON数据格式 1.对象方式:JSONObject的数据是用 { } 来表示的, 例如: { "id" : "123", & ...

随机推荐

  1. [转]Google2012.9.24校园招聘会笔试题

    代码: [cpp] view plaincopy //转载请标明出处,原文地址:http://blog.csdn.net/hackbuteer1/article/details/8017703 boo ...

  2. sleep()函数的的意义

    ===============WINDOWS平台下:====================== 关于VOID Sleep(DWORD dwMilliseconds);函数,许多人都觉得,它是告诉系统 ...

  3. jQuery autocomplete 使用

    推荐 :http://www.cnblogs.com/Peter-Zhang/archive/2011/10/22/2221147.html eg: $("#txtGrand"). ...

  4. IOS 用drawRect 画表格

    自定义一个View DrawLine DrawLine.h #import <UIKit/UIKit.h> @protocol gridTouchDelete <NSObject&g ...

  5. A10 平板开发二搭建Android开发环境

    我是直接在Ubuntu 12.10 64位系统下操作的,搭建Ubuntu开发环境类似,见Ubuntu 10.04开发环境配置.需要注意的是,64位的系统,需要安装支持32位的库(sudo apt-ge ...

  6. 【SVN Working copy is too old (format 10, created by Subversion 1.6)】解决方式

    SVN同步或者提交的时候出现类似错误信息: The working copy needs to be upgraded svn: Working copy 'D:\adt-bundle-windows ...

  7. Swift学习之类和结构体的创建

    随着一步步的学习,学习到的新知识越来越多了,不管是新的还是旧的,都禁不住时间的堆积,再熟悉的知识点时间久了都会渐渐的忘记,也许这就是人们生活中一种潜在的惰性吧,看似非常熟悉的东西,等到真正要用的时候, ...

  8. baidu地图的一个拾取坐标系统

    http://api.map.baidu.com/lbsapi/getpoint/index.html 可以很方便的通过标记取得经纬度坐标 也可以输入经纬度坐标获得地图标记

  9. alarm函数可以定时

    貌似是可以的,不过感觉好像这样用不是很好,最好还是用回timer_settimer一些列函数吧,不过既然开了头,就看下alarm怎么用吧. 1. 所需头文件  #include<unistd.h ...

  10. Cocoapods依赖管理

    对于iOS App的开发,几乎都采用了Cocoapods来管理第三方库,那么对于开发人员来说,这是必备技能,必须要掌握如何使用.这篇文章就是介绍如何安装和使用CocoaPods的. 简单来说,就是专门 ...