[Json] json-lib: simple processing of converting a java object into a json object

Source: Internet
Author: User

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 common problem in development.

Json-lib is an open-source project of sourceforge. It is often used to solve the problem of java json data conversion.

 

However, some dependent packages are required for normal use.

Commons-beanutils-1.8.0.jar


Commons-collections-3.1.jar


Commons-logging-1.1.1.jar


Commons-logging-api-1.1.jar


Ezmorph-1.0.6.jar


Commons-lang-2.4.jar !! Jar of 3.x cannot be run.


Here are the packages

 


Because the doc on the official website cannot be opened, I read others' blogs.

Here are several small cases of json conversion in java -".

Person. java javabean Model

 

[Java]
Package com. lzz. json;
 
Public class Person {
Private int id;
Private String name;
Private String addr;

Public Person (){}

Public Person (int id, String name, String addr ){
Super ();
This. id = id;
This. name = name;
This. addr = addr;
}
 
Public int getId (){
Return id;
}
 
Public void setId (int id ){
This. id = id;
}
 
Public String getName (){
Return name;
}
 
Public void setName (String name ){
This. name = name;
}
 
Public String getAddr (){
Return addr;
}
 
Public void setAddr (String addr ){
This. addr = addr;
}
}

Package com. lzz. json;

Public class Person {
Private int id;
Private String name;
Private String addr;
 
Public Person (){}
 
Public Person (int id, String name, String addr ){
Super ();
This. id = id;
This. name = name;
This. addr = addr;
}

Public int getId (){
Return id;
}

Public void setId (int id ){
This. id = id;
}

Public String getName (){
Return name;
}

Public void setName (String name ){
This. name = name;
}

Public String getAddr (){
Return addr;
}

Public void setAddr (String addr ){
This. addr = addr;
}
}

Here are several operation cases

SimpleJavaToJson. java

 

[Java]
Package com. lzz. json;
 
Import java. util. ArrayList;
Import java. util. HashMap;
Import java. util. List;
Import java. util. Map;
 
Import net. sf. json. JSONArray;
Import net. sf. json. JSONObject;
 
/*** Ref http://ryxxlong.iteye.com/blog/583862
* Json supports native js
* Using eval can be directly changed to js objects.
* JSON Java
* String <=> java. lang. String, java. lang. Character, char
* Number <=> java. lang. Number, byte, short, int, long, float, double
* True | false <=> java. lang. Boolean, boolean
* Null <=> null
* Function <=> net. sf. json. JSONFunction
* Array <=> net. sf. json. JSONArray (object, string, number, boolean, function)
* Object <=> net. sf. json. JSONObject
*/
 
Public class SimpleJavaToJson {
Public static void main (String [] args ){
/* Json objects programmed from java for the set type */
// ArrayJson ();
// ListJson ();
// StringJson ();

/* Map-type processing */
MapJson ();

/* Processing of javabean */
BeanJson ();
}

// Array Processing
Public static void arrayJson (){
String arr [] = {"lzz", "12", "ee "};
JSONArray jarr = JSONArray. fromObject (arr );
System. out. println (jarr );
}

// List Processing
Public static void listJson (){
List <String> list = new ArrayList <String> ();
List. add ("apple ");
List. add ("orange ");
List. add ("money ");
JSONArray jlist = JSONArray. fromObject (list );
System. out. println (jlist );
}

// String format the string must conform to the json set format.
Public static void stringJson (){
String str = "['A', 'B', 'C', {d: 'A'}]";
JSONArray jarr = JSONArray. fromObject (str );
System. out. println (jarr );
}

// Convert the map type to json format, and the generated json objects are not ordered.
Public static void mapJson (){
Map map = new HashMap ();
Map. put ("name", "apple ");
Map. put ("age", new Integer (20 ));
Map. put ("haoren", Boolean. TRUE );
Map. put ("arr", new String [] {"a", "B", "c "});
JSONObject jobj = JSONObject. fromObject (map );
System. out. println (jobj );
}

// Json conversion of javabean
Public static void beanJson (){
Person p = new Person (101, "apple", "China ");
JSONObject jobj = JSONObject. fromObject (p );
System. out. println (jobj );
}

}

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.