什么是json?
JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式。简单来说,json就是js对象的另一种表现形式。
json数据的格式
- json对象:
{
"size": "21",
"name": "items",
"type": "String"
}
- json数组:
[
{
"size": 20,
"name": "item1",
"type": "String"
},
{
"size": 21,
"name": "items",
"type": "String"
}
]
在项目中引入JSON-lib
在JSON-lib的官网上,明确写到:
Json-lib requires (at least) the following dependencies in your classpath(在项目中至少要有以下6的包):
net.sf.json-lib:json-lib:jar
jakarta commons-lang 2.5
jakarta commons-beanutils 1.8.0
jakarta commons-collections 3.2.1
jakarta commons-logging 1.1.1
ezmorph 1.0.6
转到下载
但是在maven项目中我们只需要一个引入即可:
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>1.0</version>
<classifier>jdk15</classifier>
</dependency>
JSON-lib的使用
在开始使用之前先新建一个JavaBean:
public class Item {
private String name;
private int size;
private String type;
//getter and setter
public Item(){}
public Item(String name, int size, String type) {
this.name = name;
this.size = size;
this.type = type;
}
@Override
public String toString() {
return "Item{" +
"name='" + name + '\'' +
", size=" + size +
", type='" + type + '\'' +
'}';
}
}
核心代码:
import com.test.po.Item;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Author: Chenyogie
* Date: 2018/1/3
* Time: 12:24
*/
public class MainDemo {
public static void main(String[] args) {
Item item1 = new Item("itemName1",21,"String");
Item item2 = new Item("itemName2",22,"Boolean");
Item item3 = new Item("itemName3",23,"Integer");
List<Item> list = new ArrayList<Item>();//构造一个List集合
list.add(item1);
list.add(item2);
list.add(item3);
Item[] items = {item1,item2,item3};//构造一个数组
Map<String,Item> map = new HashMap<String,Item>();//构造一个map集合
map.put(item1.getName(),item1);
map.put(item2.getName(),item2);
map.put(item3.getName(),item3);
//将Java数组转换成JSONArray
JSONArray jsonArray = JSONArray.fromObject(items);
System.out.println(jsonArray);
//将Java的List集合转换成JSONArray
jsonArray = JSONArray.fromObject(list);
System.out.println(jsonArray);
//将Java的Map集合转换成JSONObject
JSONObject jsonObject = JSONObject.fromMap(map);//(1)
System.out.println(jsonObject);
jsonObject = JSONObject.fromObject(map);//(2)
System.out.println(jsonObject);
//(1)(2)都可以
//将普通的JavaBean转换成JSONObject
jsonObject = JSONObject.fromObject(item1);
System.out.println(jsonObject);
//将JSONObject转换成JavaBean对象
Item item = (Item) JSONObject.toBean(jsonObject, Item.class);
System.out.println(item.toString());
//JSONObject的方法
String name = (String) jsonObject.get("name");//取得JavaBean的属性值
System.out.println(name);
//向json对象中添加JavaBean对象或者任意类型数据
JSONObject testObj1 = jsonObject.accumulate("test1", item1);
System.out.println(testObj1);
//向json对象中添加JavaBean对象或者任意类型数据
JSONObject testObj2 = jsonObject.put("test", item2);
System.out.println(testObj2);
}
}
总结:
- 从Java对象(包括集合对象)到json对象均可使用fromObject()方法,唯一不同的是调用方法的对象不一致(map集合以外常用的集合使用JSONArray,map集合及简单JavaBea使用JSONObject)。
- json对象的方法accumulate与put的异同可参见:JSONObject put,accumulate,element的区别