zoukankan      html  css  js  c++  java
  • JSONObject简介

    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

    项目结构如下:

    说明:本次用到的的文件只有工程目录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)

    5.对XML和JSON字符串各列一个简单的例子

    JSON

    {"password":"123456","username":"张三"}

    xml

    <?xml version="1.0" encoding="UTF-8"?> 
    <user_info>

    <password>123456</password>

    <username>张三</username>

    </user_info>

    start

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

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

    question1:后台接受到前台的json格式的字符串怎么处理?

    1

    question2:后台是怎么拼装json格式的字符串?

    2

    question3:json格式的字符串怎么转换为xml格式的字符串?

    3

    question4:xml格式的字符串怎么转换为json格式的字符串?

    4

    question5:javabean怎么转换为json字符串?

    5

    question6:javabean怎么转换为xml字符串?

    6

    完整的JSONObject_1_3.java代码如下:

    JSONObject_1_3

    完整的UserInfo.java代码如下:

    UserInfo

    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

    希望各位指正,欢迎拍砖。

  • 相关阅读:
    LeetCode 623. Add One Row to Tree
    LeetCode 894. All Possible Full Binary Trees
    LeetCode 988. Smallest String Starting From Leaf
    LeetCode 979. Distribute Coins in Binary Tree
    LeetCode 814. Binary Tree Pruning
    LeetCode 951. Flip Equivalent Binary Trees
    LeetCode 426. Convert Binary Search Tree to Sorted Doubly Linked List
    LeetCode 889. Construct Binary Tree from Preorder and Postorder Traversal
    LeetCode 687. Longest Univalue Path
    LeetCode 428. Serialize and Deserialize N-ary Tree
  • 原文地址:https://www.cnblogs.com/zhoushihui/p/5447164.html
Copyright © 2011-2022 走看看