您好,欢迎来到暴趣科技网。
搜索
您的当前位置:首页JSONObject与JSONArray的使用(jackson)

JSONObject与JSONArray的使用(jackson)

来源:暴趣科技网
JSONObject与JSONArray的使⽤(jackson)

1、创建⼀个JSONObject对象

package com.resource.controller.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(); jsonObject.put(\"UserName\ jsonObject.put(\"age\ jsonObject.put(\"workIn\

System.out.println(\"jsonObject1:\" + jsonObject);

//创建JsonObject第⼆种⽅法

HashMap hashMap = new HashMap(); hashMap.put(\"UserName\ hashMap.put(\"age\ hashMap.put(\"workIn\

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 arrayList = new ArrayList(); 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\ jsonObject2.put(\"age\ jsonObject2.put(\"workIn\

jsonObject2.element(\"Array\

System.out.println(\"jsonObject2:\" + jsonObject2);

}}<

结果:

jsonObject1:{\"UserName\":\"ZHULI\jsonObject2:{\"workIn\":\"ALI\jsonArray1:[\"ZHULI\jsonArray2:[\"ZHULI\

jsonArray FROM HASHMAP:[{\"workIn\":\"ALI\

jsonObject2:{\"UserName\":\"ZHULI\

2、解析JSON字符串

package com.resource.controller.web;import net.sf.json.JSONArray;import net.sf.json.JSONObject;

public class Test {

public static void main(String[] args) {

String jsonString = \"{\\\"UserName\\\":\\\"ZHULI\\\ //将Json字符串转为java对象

JSONObject obj = JSONObject.fromObject(jsonString); //获取Object中的UserName if (obj.has(\"UserName\")) {

System.out.println(\"UserName:\" + obj.getString(\"UserName\")); }

//获取ArrayObject if (obj.has(\"Array\")) {

JSONArray transitListArray = obj.getJSONArray(\"Array\"); for (int i = 0; i < transitListArray.size(); i++) {

System.out.print(\"Array:\" + transitListArray.getString(i) + \" \"); }

} }}

返回:

UserName:ZHULI

Array:ZHULI Array:30 Array: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\map.put(\"bool\map.put(\"int\map.put(\"arr\

map.put(\"func\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 = objectMapper.readValue(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 = (Person)JSONObject.toBean(obj, Person.class);//将json对象转换为Person对象

注意:当其中属性有类似List , Map ,ArrayList、⾃定义的类型,如List teachers, 就不可以了。 会报错:MorphDynaBean cannot be cast tocon.test……

在JSONObject.toBean的时候如果转换的类中有集合,可以先定义Map在classMap中put你要转换的类中的集合名,像: classMap.put(“teachers”, Teacher.class); 然后在toBean()的时候把参数加上, 像:

Student student=(Student) JSONObject.toBean(str, Student.class, classMap);

JSONObject obj = new JSONObject().fromObject(jsonStr);//将json字符串转换为json对象Map classMap = new HashMap();classMap.put(\"student\classMap.put(\"teacher\

Person pm=(Person) JSONObject.toBean(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 = json.toString();//将json对象转换为字符串

需要依赖的包:

jackson-core-asl-1.8.5.jarjackson-mapper-asl-1.8.5.jarjson-lib-2.2.1-jdk15.jar

ezmorph.jar json-lib-2.2.2-jdk15.jar

commons-lang.jar commons-beanutils.jarcommons-collections.jarcommons-logging.jar

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- baoquwan.com 版权所有 湘ICP备2024080961号-7

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务