Java中如何使用JSON-lib包解析Json格式呢?
下文讲述使用JSON-lib包解析Json格式的方法分享,如下所示:
JSON-lib包可以将一个beans,collections,maps,java arrays,XML和JSON互相转换 可以将Java对象转成json格式的字符串 也可以将Java对象转换成xml格式的文档 同样可以将json字符串转换成Java对象 或将xml字符串转换成Java对象 ---------------------------------------------- JSON-lib包下载地址 http://json-lib.sourceforge.net/
格式转换方法
list集合转换成json方法
List list = new ArrayList(); list.add( "aaa" ); list.add( "bbb" ); JSONArray jsonArray2 = JSONArray.fromObject( list );
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);
Bean转换成json方法
JSONObject jsonObject = JSONObject.fromObject(new JsonBean());
数组转换成json方法
boolean[] boolArray = new boolean[] { true, false, true }; JSONArray jsonArray1 = JSONArray.fromObject(boolArray);
字符串转换成json方法
JSONArray jsonArray3 = JSONArray.fromObject("['json','is','easy']" );
beans转换成json方法
List list = new ArrayList(); JsonBean2 jb1 = new JsonBean2(); jb1.setCol(1); jb1.setRow(1); jb1.setValue("aaa"); JsonBean2 jb2 = new JsonBean2(); jb2.setCol(2); jb2.setRow(2); jb2.setValue("bbb"); list.add(jb1); list.add(jb2); JSONArray ja = JSONArray.fromObject(list);
JSONObject转换DynaBean方法
String json = "{name=\"json\",bool:true,int:1,double:2.2}"; JSONObject jsonObject = JSONObject.fromObject(json); //抽象的写法:DynaBean bean = (DynaBean) JSONSerializer.toJava( jsonObject ); Object bean = JSONObject.toBean(jsonObject); //Object bean1 = JSONSerializer.toJava(jsonObject); assertEquals(jsonObject.get("name"), PropertyUtils.getProperty(bean, "name")); assertEquals(jsonObject.get("bool"), PropertyUtils.getProperty(bean, "bool")); assertEquals(jsonObject.get("int"), PropertyUtils.getProperty(bean, "int")); assertEquals(jsonObject.get("double"), PropertyUtils.getProperty(bean, "double"));
JSONObject转换JavaBean方法
String json = "{name:\"zhangsan\",age:25,hight:1.72,sex:true}"; JSONObject jsonObject = JSONObject.fromObject(json); UserBean bean = (UserBean) JSONObject.toBean(jsonObject, UserBean.class); System.out.println(jsonObject);
JSONArray转换List方法
String json = "[\"first\",\"second\"]"; JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON(json); List output = (List) JSONSerializer.toJava(jsonArray);
JSONArray转换array方法
String json = "[\"first\",\"second\"]"; JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON(json); JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setArrayMode(JsonConfig.MODE_OBJECT_ARRAY); Object[] output = (Object[]) JSONSerializer.toJava(jsonArray, jsonConfig); Object[] expected = new Object[] { "first", "second" }; ArrayAssertions.assertEquals(expected, output);
Arrays转换Json方法
@Test public void array2json() { int[] intArray = new int[] { 1, 4, 5 }; JSONArray jsonArray1 = JSONArray.fromObject(intArray); System.out.println("int[] intArray"); System.out.println(jsonArray1); boolean[] boolArray = new boolean[] { true, false, true }; System.out.println("boolean[] boolArray"); JSONArray jsonArray2 = JSONArray.fromObject(boolArray); System.out.println(jsonArray2); int[][] int2Array = new int[][] { { 1, 2 }, { 3, 4 } }; JSONArray jsonArray3 = JSONArray.fromObject(int2Array); System.out.println("int[][] int2Array"); System.out.println(jsonArray3); float[] floatArray = new float[] { 0.1f, 0.2f, 0.3f }; JSONArray jsonArray4 = JSONArray.fromObject(floatArray); System.out.println("float[] floatArray"); System.out.println(jsonArray4); String[] strArray = new String[] { "xiaoxiao", "liuliu", "zhangzhang" }; JSONArray jsonArray5 = JSONArray.fromObject(strArray); System.out.println("String[] strArray"); System.out.println(jsonArray5); }
Collections转换Json方法
@Test public void collections2json() { List list1 = new ArrayList(); list1.add("first"); list1.add("second"); JSONArray jsonArray1 = JSONArray.fromObject(list1); System.out.println("List list1"); System.out.println(jsonArray1); List < User > list2 = new ArrayList < User > (); list2.add(new User("xiaoming", "男", 6)); list2.add(new User("xiaozhang", "女", 7)); list2.add(new User("xiaoliu", "男", 8)); JSONArray jsonArray2 = JSONArray.fromObject(list2); System.out.println("List<User> list2"); System.out.println(jsonArray2); }
Xml转换Json方法
@Test public void xml2json() { String s = "<user> <name id='1'>xiaoxiao</name> <sex class='s1'>男</sex> <age>6</age> </user>"; XMLSerializer x = new XMLSerializer(); JSON json = x.read(s); System.out.println("XmlToJson"); System.out.println(json.toString()); }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。