Essays-46 Articles-0 comments-132
Jsonobject Introduction
This section summarizes : A brief introduction to JSON before, and a simple comparison of JSON and XML, then I think, if it is a JSON-formatted string to the background, how do I need to deal with it? If the foreground page needs to be returned in the background is a JSON string, then the background is the JSON format of the string to be assembled together? Is there a connection between JSON and XML that can be exchanged between them? ...... With these questions, search the relevant information, and their own hands-on operation, finally to these problems have a relatively clear understanding. These problems are mainly implemented by Jsonobject The plugin's jar package.
Preparation1. Jsonobject Introduction
The Jsonobject-lib package is a Beans,collections,maps,java arrays and XML and JSON transforms each other's packages.
2. Download the jar Package
Http://files.cnblogs.com/java-pan/lib.rar
Provides 6 additional jar packages that are dependent on the jar other than Jsonobject, with a total of 7 jar files
Note: This blog is based on version 1.1 because the version of the project used in the work is 1.1 of the corresponding jdk1.3 version.
The Javadoc download path corresponding to this version is as follows:HTTP://SOURCEFORGE.NET/PROJECTS/JSON-LIB/FILES/JSON-LIB/JSON-LIB-1.1/
Currently the latest version is 2.4 and the other version is http://sourceforge.net/projects/json-lib/files/json-lib/
3. Project Environment:
system:win7 myeclipse:6.5 tomcat:5.0 JDK: Both the development environment and the compilation are 1.5
The project structure is as follows:
Note : The files used in this session are only jsonobject_1_3 classes and Note.txt under the project directory JSON package
4.class&method based onAPI for 1.1
Make the following conventions:
1. Introduction of API based on Jsonobject 1.1
2. Introduce only the commonly used classes and methods
3. No longer recommended for use in this release
4. The classes and methods introduced are mainly used in this blog
Jsonobject: A Jsonobject is an unordered collection of name/value pairs.
is a final class, inherits the object, implements the JSON interface
The construction method is as follows:
Jsonobject (); Create an empty Jsonobject object
Jsonobject (Boolean isNull); Create an empty Jsonobject object
The general method is as follows:
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. optThe A method returns a default value instead of throwing an exception, and so are useful for obtaining optional values.
Jsonarray: A Jsonarray is an ordered sequence of values.
is a final class, inherits the object, implements the JSON interface
The construction method is as follows:
JSONArray();构造一个空的JSONArray对象
The general method is as follows:
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 a back.
A class that inherits from Object
The construction method is as follows:
XMLSerializer();创建一个XMLSerializer对象
The general method is as follows:
setRootName(String rootName);设置转换的xml的根元素名称
setTypeHintsEnabled(boolean typeHintsEnabled);设置每个元素是否显示type属性
write(JSON json);把json对象转换为xml,默认的字符编码是UTF-8,
需要设置编码可以用write(JSON json, String encoding)
5. A simple example of each column of XML and JSON strings
JSON:
{"Password": "123456", "username": "Zhang San"}
Xml
<?xml version= "1.0" encoding= "UTF-8"?>
<user_info>
<password>123456</password>
<username> Zhang San </username>
</user_info>
Start
Create a new Web project, project name JS, import the following 7 jar packages, the files in the previous preparation to download the path.
Note : You can not create a new Web project, the normal Java project can also complete the operation of this article. As for why to import 6 packages outside of the JSON package, I will paste the note.txt in the end, you will see.
Question1: How do I handle a JSON-formatted string that receives to the foreground in the background?
1
Question2: How do you assemble a JSON-formatted string in the background?
2
How does a string in Question3:json format be converted to an XML-formatted string?
3
How does a string in question4:xml format be converted to a JSON-formatted string?
4
How does Question5:javabean convert to a JSON string?
5
How does Question6:javabean convert to an XML string?
6
The complete Jsonobject_1_3.java code is as follows:
jsonobject_1_3
The complete Userinfo.java code is as follows:
UserInfo
Result
Both the code and the results of the operation have been posted behind each problem, and the runtime can see the results of the test directly using the main method to run each method separately.
Note.txt is the corresponding error and solution of the report, and also explains why the jar package mentioned above should be imported in another way;
The contents of the Note.txt file are as follows:
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"
Solution: Import 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"
Solution: Import 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"
Solution: Import 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"
Solution: Import 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"
Solution: Import 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)
Solution: Import Xom-1.0d10.jar
A few notes:
1. Note the modifiers of the UserInfo class, modified with public, the variables username and password are also decorated with public, preferably write a separate class, which is not posted here
2. The above JSON strings and XML strings are the simplest form, and the actual development of JSON strings and XML format is more complex than this,
Processing complex JSON strings, you can encapsulate writing a class to inherit HashMap, and then rewrite its put and get methods to support the type of a[0]. Reading and specifying key values for B and a.b
3. The above 6 situation may not exist or be used in actual development
Problems that exist:
1. Using the XmlSerializer write method to generate an XML string with the Chinese garbled problem
Red log log issues in 2.question4
2012-4-6 15:04:35 Net.sf.json.xml.XMLSerializer GetType
Info: Using The default type string
I hope you correct me, welcome to shoot bricks.
"Go" jsonobject use method