[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.

Is http://sourceforge.net/projects/json-lib/

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

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

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 object * 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) {/* Set-type JSON object programmed from Java * // arrayjson (); // listjson (); // stringjson ();/* map-type processing */mapjson ();/* JavaBean Processing */beanjson ();} // Array Processing Public static void arrayjson () {string arr [] = {"lzz", "12", "ee"}; jsonarray jarr = jsonarray. fromobject (ARR); system. out. println (jarr);} // list 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 public static void stringjson () {string STR = "['A', 'B ', 'C', {d: 'A'}] "; jsonarray jarr = jsonarray. fromobject (STR); system. out. println (jarr);} // converts the map type to JSON format. The generated JSON object is also public static void mapjson () {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);} // convert the JSON format of Javabean to public static void beanjson () {person P = new person (101, "apple", "China "); jsonobject jobj = jsonobject. fromobject (p); system. out. println (jobj );}}
Related Article

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.