JSONObject与JSONArray的使⽤(jackson)1、创建⼀个JSONObject对象
结果:ller.web;
import java.util.ArrayList;
import java.util.HashMap;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class Test {
public static void main(String[] args) {
//JsonObject和JsonArray区别就是JsonObject是对象形式,JsonArray是数组形式
//创建JsonObject第⼀种⽅法
JSONObject jsonObject = new JSONObject();
object to
jsonObject.put("UserName", "ZHULI");
jsonObject.put("age", "30");
jsonObject.put("workIn", "ALI");
System.out.println("jsonObject1:" + jsonObject);
//创建JsonObject第⼆种⽅法
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("UserName", "ZHULI");
hashMap.put("age", "30");
hashMap.put("workIn", "ALI");
System.out.println("jsonObject2:" + JSONObject.fromObject(hashMap));
//创建⼀个JsonArray⽅法1
JSONArray jsonArray = new JSONArray();
jsonArray.add(0, "ZHULI");
jsonArray.add(1, "30");
jsonArray.add(2, "ALI");
System.out.println("jsonArray1:" + jsonArray);
//创建JsonArray⽅法2
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("ZHULI");
arrayList.add("30");
arrayList.add("ALI");
System.out.println("jsonArray2:" + JSONArray.fromObject(arrayList));
/
/如果JSONArray解析⼀个HashMap,则会将整个对象的放进⼀个数组的值中
System.out.println("jsonArray FROM HASHMAP:" + JSONArray.fromObject(hashMap));
//组装⼀个复杂的JSONArray
JSONObject jsonObject2 = new JSONObject();
jsonObject2.put("UserName", "ZHULI");
jsonObject2.put("age", "30");
jsonObject2.put("workIn", "ALI");
jsonObject2.element("Array", arrayList);
System.out.println("jsonObject2:" + jsonObject2);
}
}
jsonObject1:{"UserName":"ZHULI","age":"30","workIn":"ALI"}
jsonObject2:{"workIn":"ALI","age":"30","UserName":"ZHULI"}
jsonArray1:["ZHULI","30","ALI"]
jsonArray2:["ZHULI","30","ALI"]
jsonArray FROM HASHMAP:[{"workIn":"ALI","age":"30","UserName":"ZHULI"}]
jsonObject2:{"UserName":"ZHULI","age":"30","workIn":"ALI","Array":["ZHULI","30","ALI"]}
2、解析JSON字符串
ller.web;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class Test {
public static void main(String[] args) {
String jsonString = "{\"UserName\":\"ZHULI\",\"age\":\"30\",\"workIn\":\"ALI\",\"Array\":[\"ZHULI\",\"30\",\"ALI\"]}";
//将Json字符串转为java对象
JSONObject obj = JSONObject.fromObject(jsonString);
//获取Object中的UserName
if (obj.has("UserName")) {
System.out.println("UserName:" + String("UserName"));
}
//获取ArrayObject
if (obj.has("Array")) {
JSONArray transitListArray = JSONArray("Array");
for (int i = 0; i < transitListArray.size(); i++) {
System.out.print("Array:" + String(i) + " ");
}
}
}
}
返回:
UserName:ZHULI
Array:ZHULI Array:30Array:ALI
3、基本⽅法介绍
(1)List集合转换成json⽅法
List list = new ArrayList();
list.add( "first" );
list.add( "second" );
JSONArray jsonArray2 = JSONArray.fromObject( list );
(2)Map集合转换成json⽅法
Map map = new HashMap();
map.put("name", "json");
map.put("bool", Boolean.TRUE);
map.put("int", new Integer(1));
map.put("arr", new String[] { "a", "b" });
map.put("func", "function(i){ return this.arr[i]; }");
JSONObject json = JSONObject.fromObject(map);
(3)Bean转换成json代码
JSONObject jsonObject = JSONObject.fromObject(new JsonBean());
(4)数组转换成json代码
boolean[] boolArray = new boolean[] { true, false, true };
JSONArray jsonArray1 = JSONArray.fromObject(boolArray);
(5)⼀般数据转换成json代码
JSONArray jsonArray3 = JSONArray.fromObject("['json','is','easy']" );
(6)beans转换成json代码
List list = new ArrayList();
JsonBean2 jb1 = new JsonBean2();
jb1.setCol(1);
jb1.setRow(1);
jb1.setValue("xx");
JsonBean2 jb2 = new JsonBean2();
jb2.setCol(2);
jb2.setRow(2);
jb2.setValue("");
list.add(jb1);
list.add(jb2);
JSONArray ja = JSONArray.fromObject(list);
4、与org.json⽐较
json-lib和org.json的使⽤⼏乎是相同的,我总结出的区别有两点:
1. org.json⽐json-lib要轻量得多,前者没有依赖任何其他jar包,⽽后者要依赖ezmorph和commons的lang、logging、beanutils、
collections等组件
2. json-lib在构造bean和解析bean时⽐org.json要⽅便的多,json-lib可直接与bean互相转换,⽽org.json不能直接与bean相互转换⽽
需要map作为中转,若将bean转为json数据,⾸先需要先将bean转换为map再将map转为json,⽐较⿇烦。
5、实体类和JSON对象之间相互转化(依赖包jackson-all-1.7.6.jar、jsoup-1.5.2.jar)
(1)将json转化为实体POJO:
①POJO的字段可以多于json的字段值,若少于则报错:Unrecognized field “name” (Class test.json.Student), not marked as ignorable
ObjectMapper objectMapper = new ObjectMapper();
T t = adValue(jsonStr, obj);
②先将json字符串转换为json对象,再将json对象转换为java对象———-推荐使⽤
(不存在上述异常,会警告: Tried to assign property age:java.lang.String to bean of class test.json.Person)
JSONObject obj = new JSONObject().fromObject(jsonStr);//将json字符串转换为json对象
Person person = (Bean(obj, Person.class);//将json对象转换为Person对象
注意:当其中属性有类似List , Map ,ArrayList、⾃定义的类型,如List teachers, 就不可以了。 会报错:MorphDynaBean cannot be cast st……
在Bean的时候如果转换的类中有集合,可以先定义Map
在classMap中put你要转换的类中的集合名,像:
classMap.put(“teachers”, Teacher.class);
然后在toBean()的时候把参数加上, 像:
Student student=(Student) Bean(str, Student.class, classMap);
JSONObject obj = new JSONObject().fromObject(jsonStr);//将json字符串转换为json对象Map<String, Class> classMap = new HashMap<String, Class>();
classMap.put("student", Student.class);
classMap.put("teacher", Teacher.class);
Person pm=(Person) Bean(obj, Person.class, classMap);
(2)将实体POJO转化为JSON:
①⽤writeValueAsString
ObjectMapper mapper = new ObjectMapper();
String jsonStr = mapper.writeValueAsString(obj);
②先将java对象转换为json对象,在将json对象转换为json字符串
JSONObject json = JSONObject.fromObject(obj);//将java对象转换为json对象
String str = String();//将json对象转换为字符串
需要依赖的包:
jackson-core-asl-1.8.5.jar
jackson-mapper-asl-1.8.5.jar
json-lib-2.2.1-jdk15.jar
ezmorph.jar json-lib-2.2.2-jdk15.jar
commons-lang.jar commons-beanutils.jar
commons-collections.jar
commons-logging.jar
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论