1. Json-lib can easily turn Java objects into Json-formatted strings, using tutorials (http://json-lib.sourceforge.net/usage.html)
But when there is a time type in the Java object. Json-lib converts the time field into:
{"Date": "Day": 3, "hours": 0,
When you use Json-lib to convert a Java.util.Date object directly into a JSON string, you usually get a format that is not what you want:System.out.println (Jsonserializer.tojson (new Date () )); // {"Date": +, "Day": 3, "hours": minutes, "month": 5
Here is a brief introduction to how to use the Json-lib Toolkit to complete the transformation between a Java object (or collection) and a JSON object (or collection) ~1. Translating Java objects into JSON (creating JSON)Key class: Jsonobject
Thanks to JS's native support for JSON, JSON is widely used for data transmission in many projects. How can we convert data from a Java object to a JSON object? How can we convert the foreground JSON object to a Java object for processing?
This is a
1Demo json-Lib Small tool2 Public classDemo1 {3 /*4 * When map comes in5 */6 @Test7 Public voidfun1 () {8Jsonobject map =NewJsonobject ();9Map.put ("name", "Zhangsan");TenMap.put ("Age", 23); OneMap.put ("Sex", "male"); A
When using Jsonobject.fromobject (BEAN,CFG) in the Json-lib package, the following situations may occur:1, (prevent self-contained) the converted object contains its own object, or object A is suspended under object B, Object B is also suspended
1.XStream Tool Description:This tool, the jar package for this tool, can help us to generate XML-formatted data such as JavaBean, set (LIST,SET,MAP), etc.Jar Package:Xstream-1.4.4.jar, this is the core package.Xpp3_min-1.1.4c.jar This is a
Solution1. Set the JSON-LIB to let it filter out fields that cause loops.Java codeCopy codeThe Code is as follows:JsonConfig config = new JsonConfig ();Config. setIgnoreDefaultExcludes (false );Config. setCycleDetectionStrategy
First, to prevent the package self-containedIn Json-lib, when the object to be converted contains its own object, an exception is thrown there is a cycle in the hierarchy, the workaround:Jsonconfig.setcycledetectionstrategy
Class (parsing and generation of Java JSON):ImportJava.util.HashMap;ImportJava.util.Map;ImportNet.sf.json.JSONArray;ImportNet.sf.json.JSONObject; Public classJsontest { PublicString jsontostring () {Jsonobject users=NewJsonobject (); Jsonarray Array=
1, first of all to understand a few conceptsJSON object, JSON array, JSON stringJSON objects can be represented by jsonobjectThe JSON array can be represented by JsonarrayA JSON string is just a string of typeAnd then look at the following code that'
In order to parse the JSON data in the program, the json-lib-2.3-jdk15.jar is online. This package encapsulates the JSON parsing method. To use this package, you must also support the following packages.
Commons-lang.jar; extends the java. lang
Use json-lib to parse json data
1. First, we need to understand several concepts.
Json object, json array, json string
Json objects can be represented by JsonObject.
Json array can be represented by JsonArray
A json String is a String of the String
PS: Look at this blog before you first introduce the toolkit Json-lib-2.2.2-jdk15.jarResources Link: Baidu Cloud: Link: https://pan.baidu.com/s/1o9k7PSu Password: 00LJOne, from object to stringTo construct a jsonobject or Jsonarray object with
Json-lib.jar Development Kit Uses:Dependent Packages:Commons-beanutils.jar;Commons-httpclient.jar;Commons-lang.jar;Ezmorph.jar, a lot of people use will prompt net.sf.ezmorph.xxx can not find, is missing this:Morph-1.0.1.jarRELATED Links:http://json-
When a JSON string goes to a Java bean, a generic object can be transferred directly, such as: A student class, attributes have name, age, etc. Public classstudent{PrivateString sname; PrivateInteger age; PublicString Getsname () {returnsname; }
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.