JSONObject简介(2)

JSONObject简介

本节摘要:之前对JSON做了一次简单的介 绍,并把JSON和XML做了一个简单的比较;那么,我就在想,如果是一个json格式的字符串传到后台,需要怎么对其处理?如果前台页面需要后台返回的 是一个json的字符串,那么后台是怎么把json格式的字符串给拼装起来的呢?JSON和XML是不是有某种联系,他们之间是不是可以互相转换?……带 着这些问题,搜索了相关的资料,并自己实际动手操作,总算对这些问题有了个比较清晰的认识。这些问题主要是通过JSONObject这个插件的jar包实现。

preparation

1.JSONObject介绍

JSONObject-lib包是一个beans,collections,maps,java arrays和xml和JSON互相转换的包。

2.下载jar包

http://files.cnblogs.com/java-pan/lib.rar

提供了除JSONObject的jar之外依赖的其他6个jar包,一共7个jar文件

说明:因为工作中项目用到的版本是1.1的对应jdk1.3的版本,故本篇博客是基于1.1版本介绍的。

对应此版本的javadoc下载路径如下:http://sourceforge.net/projects/json-lib/files/json-lib/json-lib-1.1/

目前最新的版本为2.4,其他版本下载地址为http://sourceforge.net/projects/json-lib/files/json-lib/

3.项目环境:

system:WIN7 myeclipse:6.5 tomcat:5.0 JDK:开发环境和编译用的都是1.5

项目结构如下:

JSONObject简介(2)

说明:本次用到的的文件只有工程目录json包下的JSONObject_1_3类和note.txt

4.class&method 基于1.1的API

做以下几点约定:

1.介绍基于JSONObject 1.1的API

2.只介绍常用的类和方法

3.不再介绍此版本中已经不再推荐使用

4.介绍的类和方法主要围绕本篇博客中用到的

JSONObject:A JSONObject is an unordered collection of name/value pairs.

是一个final类,继承了Object,实现了JSON接口

构造方法如下:

JSONObject();创建一个空的JSONObject对象

JSONObject(boolean isNull);创建一个是否为空的JSONObject对象

普通方法如下:

fromBean(Object bean);静态方法,通过一个pojo对象创建一个JSONObject对象

fromJSONObject(JSONObject object);静态方法,通过另外一个JSONObject对象构造一个JSONObject对象

fromJSONString(JSONString string);静态方法,通过一个JSONString创建一个JSONObject对象

toString();把JSONObject对象转换为json格式的字符串

iterator();返回一个Iterator对象来遍历元素

接下来就是一些put/get方法,需要普通的get方法和pot方法做一下强调说明,API中是这样描述的:

A get method returns a value if one can be found, and throws an exception if one cannot be found. An opt method returns a default value instead of throwing an exception, and so is useful for obtaining optional values.

JSONArray:A JSONArray is an ordered sequence of values.

是一个final类,继承了Object,实现了JSON接口

构造方法如下:

JSONArray();构造一个空的JSONArray对象

普通方法如下:

fromArray(Object[] array);静态方法,通过一个java数组创建一个JSONArray对象

fromCollection(Collection collection);静态方法,通过collection集合对象创建一个JSONArray对象

fromString(String string);静态方法,通过一个json格式的字符串构造一个JSONArray对象

toString();把JSONArray对象转换为json格式的字符串

iterator();返回一个Iterator对象来遍历元素

接下来同样是put/get方法……

XMLSerializer:Utility class for transforming JSON to XML an back.

一个继承自Object的类

构造方法如下:

XMLSerializer();创建一个XMLSerializer对象

普通方法如下:

setRootName(String rootName);设置转换的xml的根元素名称

setTypeHintsEnabled(boolean typeHintsEnabled);设置每个元素是否显示type属性

write(JSON json);把json对象转换为xml,默认的字符编码是UTF-8,

需要设置编码可以用write(JSON json, String encoding)

start

新建web工程,工程名称JS,导入以下7个jar包,文件在前面的准备工作中下载路径。

说明:可以不用新建web工程,普通的java工程也可以完成本篇的的操作。至于为什么要导入处json包以外的其他6个包,我会把note.txt贴在最后,各位一看便知。

package foo;

import net.sf.json.JSON;
import net.sf.json.JSONObject;
import net.sf.json.xml.XMLSerializer; public class JSONObject_1_3 {
public static void javaToJSON() {
System.out.println("java代码封装为json字符串");
JSONObject jsonObj = new JSONObject();
jsonObj.put("username", "张三");
jsonObj.put("password", "123456");
System.out.println("java--->json \n" + jsonObj.toString());
} public static void jsonToJAVA() {
System.out.println("json字符串转java代码");
String jsonStr = "{\"password\":\"123456\",\"username\":\"张三\"}";
JSONObject jsonObj = JSONObject.fromObject(jsonStr);
String username = jsonObj.getString("username");
String password = jsonObj.optString("password");
System.out.println("json--->java\n username=" + username
+ "\t password=" + password);
} public static void jsonToXML() {
System.out.println("json字符串转xml字符串");
String jsonStr = "{\"password\":\"123456\",\"username\":\"张三\"}";
JSONObject json = JSONObject.fromObject(jsonStr);
XMLSerializer xmlSerializer = new XMLSerializer();
xmlSerializer.setRootName("user_info");
xmlSerializer.setTypeHintsEnabled(false);
String xml = xmlSerializer.write(json);
System.out.println("json--->xml \n" + xml);
} public static void javaBeanToJSON() {
System.out.println("javabean转json字符串");
UserInfo userInfo = new UserInfo();
userInfo.setUsername("张三");
userInfo.setPassword("123456");
JSONObject json = JSONObject.fromObject(userInfo);
System.out.println("javabean--->json \n" + json.toString());
} public static void javaBeanToXML() {
System.out.println("javabean转xml字符串");
UserInfo userInfo = new UserInfo();
userInfo.setUsername("张三");
userInfo.setPassword("123456");
JSONObject json = JSONObject.fromObject(userInfo);
XMLSerializer xmlSerializer = new XMLSerializer();
String xml = xmlSerializer.write(json, "UTF-8");
System.out.println("javabean--->xml \n" + xml);
} public static void xmlToJSON() {
System.out.println("xml字符串转json字符串");
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><user_info><password>123456</password><username>张三</username></user_info>";
XMLSerializer x=new XMLSerializer();
JSON json = x.read(xml);
System.out.println("xml--->json \n" + json.toString());
} public static void Stringtobean() { String jsonStr = "{\"password\":\"123456\",\"username\":\"张三\"}";
JSONObject json = JSONObject.fromObject(jsonStr);
UserInfo userInfo = (UserInfo)JSONObject.toBean(json, UserInfo.class);
System.out.println(userInfo.getUsername());
} public static void main(String args[]) {
javaToJSON();
jsonToJAVA();
jsonToXML();
xmlToJSON();
// //bean的方法有报错,待解决
// javaBeanToJSON();
// javaBeanToXML();
// Stringtobean();
}
} class UserInfo {
public String username;
public String password; public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} }

结果

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAj4AAAEOCAIAAAAGworLAAAgAElEQVR4nO2d67WsqhJGjYuAiIcQThQkQxI7A88PFQsoFNtHS685xx3nrm2rPNT6LMCq4R8AAEBXDP/+/fsPAACgH4Z///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uEAAAL9SURBVBqkS0FPJiI8jwMJSmJ4jjySr4hdn50zKX4vyEa1Ba0jQ3WTqn1tlu5dip96tjve9CuRRFrzxaid3Hrdq7lyDn3t0H6NgnN++k8xWpxijGspFukCeDmadB0yKlXXY30tLs9XblyM9/zLfAI9MmEyQSEi7UqT9YR0FSqVjxcujapFsRJNM85fnFdMly7tcshtq4jWOnkricxSUCVXjl7QVgsa30HmsT/nvT3fe4wYArwfKV3t5lzaIGX0Rr5Vq1PfatYS48LorTFm0qxyqC6WqthfaW+uz96sna4W4CNuX2tZHyRcYwdLmbjEcurSpVyO3DlZpWtL5PIJxeS666EvKwWdbKYvbsSafk1uWQvJBQGA96Hl62ryukrpEjZXX+tRmHq50dvB+uCs885YPxXwWumqZGaRVVFGq7QcZItXlvpqV9R+M/Rw1vOalW6WLuW669J1hzsTglt10/r57WuZ0ko0dTDO+YaOxesCeD+fz3WlS/iKAb36aJK+MThjrLUujMFZa9cI9fqAoTbFJCfHbhww3NQttQN1LRUjcplbdNVI2maHJNrTNKirS5d23XXpOhqJsvkaLbNcccLrFMx1Abyfj6VLcSXE6m63PP7q2gl142rJE21Yi0oKqoy/xXfvS6QrfWlfrXKxcWtt/NiwTOLg+oU2dY6n3Lsc+gRc+4Bhed0r0qUXVKVZuryb3nOc9y62VXN7W4tFugBezmmvC77AQffll1m7QrqRZ0b8uLkB3k/DJ8kKPN1fY3ImmIy5Daa6ADpACQSlrglMQbrgB2m48wHgFRB+FwAAOgPpAgCAzkC6AACgM5AuAADoDKQLAAA6A+kCAIDOQLoAAKAzkC4AAOgMpAsAADoD6QIAgM5AugAAoDOQLgAA6AykCwAAOgPpAgCAzkC6AACgM5AuAADoDKQLAAA6A+kCAIDOQLoAAKAzkC4AAOgMpAsAADoD6QIAgM5AugAAoDOQLgAA6AykCwAAOgPpAgCAzkC6AACgM5AuAADojP8BJUFL2jSWjTwAAAAASUVORK5CYII=" alt="" />

result

代码和运行结果都已经贴在每个问题的后面,运行时直接用main方法分别对每个方法运行即可看到测试效果。

note.txt是报的对应的错误及解决方法,也从另一个方面说明为什么需要导入前面提到的jar包;

note.txt文件内容如下:

java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntimeException
at
java.lang.ClassLoader.defineClass0(Native Method)
at
java.lang.ClassLoader.defineClass(ClassLoader.java:537)
at
java.security.SecureClassLoader.defineClass(SecureClassLoader.java:123)

at java.net.URLClassLoader.defineClass(URLClassLoader.java:251)
at
java.net.URLClassLoader.access$100(URLClassLoader.java:55)
at
java.net.URLClassLoader$1.run(URLClassLoader.java:194)
at
java.security.AccessController.doPrivileged(Native Method)
at
java.net.URLClassLoader.findClass(URLClassLoader.java:187)
at
java.lang.ClassLoader.loadClass(ClassLoader.java:289)
at
sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)
at
java.lang.ClassLoader.loadClass(ClassLoader.java:235)
at
java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
at
generate.TestJSONObject.main(TestJSONObject.java:40)
Exception in thread
"main"
解决方案:导入commons-lang-2.1.jar

java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory

at net.sf.json.JSONObject.<clinit>(JSONObject.java:125)
at
generate.TestJSONObject.main(TestJSONObject.java:40)
Exception in thread
"main"
解决方案:导入commons-logging.jar

java.lang.NoClassDefFoundError: org/apache/commons/beanutils/DynaBean

at net.sf.json.JSONObject.set(JSONObject.java:2164)
at
net.sf.json.JSONObject.put(JSONObject.java:1853)
at
net.sf.json.JSONObject.put(JSONObject.java:1806)
at
generate.TestJSONObject.main(TestJSONObject.java:41)
Exception in thread
"main"
解决方案:导入commons-beanutils.jar

java.lang.NoClassDefFoundError: net/sf/ezmorph/MorpherRegistry
at
net.sf.json.util.JSONUtils.<clinit>(JSONUtils.java:65)
at
net.sf.json.JSONObject.set(JSONObject.java:2164)
at
net.sf.json.JSONObject.put(JSONObject.java:1853)
at
net.sf.json.JSONObject.put(JSONObject.java:1806)
at
generate.TestJSONObject.main(TestJSONObject.java:41)
Exception in thread
"main"
解决方案:导入ezmorph-1.0.2.jar

java.lang.NoClassDefFoundError: org/apache/commons/collections/FastHashMap

at
org.apache.commons.beanutils.PropertyUtils.<clinit>(PropertyUtils.java:208)

at net.sf.json.JSONObject.fromBean(JSONObject.java:190)
at
net.sf.json.JSONObject.fromObject(JSONObject.java:437)
at
net.sf.json.JSONObject.set(JSONObject.java:2196)
at
net.sf.json.JSONObject.put(JSONObject.java:1853)
at
net.sf.json.JSONObject.put(JSONObject.java:1806)
at
generate.TestJSONObject.main(TestJSONObject.java:41)
Exception in thread
"main"
解决方案:导入commons-collections-3.0.jar

Exception in thread "main" java.lang.NoClassDefFoundError: nu/xom/Serializer

at generate.TestJSONObject.jsonToXML(TestJSONObject.java:88)
at
generate.TestJSONObject.main(TestJSONObject.java:96)

解决方案:导入xom-1.0d10.jar

几点说明:

1.注意UserInfo类的修饰符,用public修饰,变量username和password也用public修饰,最好单独的写一个类,这里就不贴出来了

2.以上json字符串和xml字符串都是最简单的形式,实际开发中json字符串和xml格式比这个复杂的多,

处理复杂的json字符串,可以封装写一个类继承HashMap,然后重写其put和get方法,以支持对类型为A[0].B及A.B的键值的读取和指定

3.以上6中情况在实际开发中可能有些不存在或不常用

存在的问题:

1.使用XMLSerializer的write方法生成的xml字符串的中文乱码问题

2.question4中的红色的log日志问题

2012-4-6 15:04:35 net.sf.json.xml.XMLSerializer getType
信息: Using default
type string

3.转换bean的方法报错

上一篇:配置Tomcat出现Unsupported major.minor version 51.0


下一篇:SQL注入之Sqli-labs系列第四十一关(基于堆叠注入的盲注)和四十二关四十三关四十四关四十五关