Test code for Json data in Java

Source: Internet
Author: User
Tags json

JSON is a very simple and important data format, which is usually used to exchange and transmit data. It is widely used in JavaScript technology and is becoming increasingly popular in various popular programming languages. There is also a JSON Library in Java, which is used to convert Java objects to JSON and XML data and has an important application.

Open source JSON Library home page: http://json-lib.sourceforge.net/

Environment: JDK5, json-lib-2.3-jdk15

Dependent packages: json-lib-2.3-jdk15.jar, commons-collections.jar, commons-lang. jar, commons-logging.jar, commons-beanutils.jar, ezmorph-1.0.6.jar, xom-1.1.jar

Json format corresponding to various types in java:

1. Array or set --> JSON string

The code is as follows: Copy code

Public static void test1 (){

System. out. println ("------------ array or set --> JSON string ----------");

Boolean [] boolArray = new boolean [] {true, false, true };

JSONArray jsonArray1 = JSONArray. fromObject (boolArray );

System. out. println (jsonArray1 );

// Output format: [true, false, true]

List list = new ArrayList ();

List. add ("first ");

List. add ("second ");

JSONArray jsonArray2 = JSONArray. fromObject (list );

System. out. println (jsonArray2 );

// Output format: ["first", "second"]

JSONArray jsonArray3 = JSONArray. fromObject ("['json', 'is', 'easy']");

System. out. println (jsonArray3 );

// Output format: ["json", "is", "easy"]

}

2. Object | Map --> JSON string

The code is as follows: Copy code

Public static void test2 (){

System. out. println ("------------ Object | Map --> JSON string ----------");

Map map = new HashMap ();

Map. put ("name", "json ");

Map. put ("bool", Boolean. TRUE );

Map. put ("int", new Integer (1 ));

Map. put ("arr", new String [] {"a", "B "});

Map. put ("func", "function (I) {return this. arr [I];}");

JSONObject jsonObject1 = JSONObject. fromObject (map );

System. out. println (jsonObject1 );

// Output format: {"func": function (I) {return this. arr [I] ;}, "arr": ["a", "B"], "int": 1, "bool": true, "name ": "json "}

JSONObject jsonObject2 = JSONObject. fromObject (new MyBean ());

System. out. println (jsonObject2 );

// Output format: {"func1": function (I) {return this. options [I];}, "func2": function (I) {return this. options [I];}, "name": "json", "options": ["a", "f"], "pojoId": 1}

}

Public class MyBean {

Private String name = "json ";

Private int pojoId = 1;

Private char [] options = new char [] {'a', 'F '};

Private String func1 = "function (I) {return this. options [I];}";

Private JSONFunction func2 = new JSONFunction (new String [] {"I"}, "return this. options [I];");

Public String getName (){

Return name;

}

Public void setName (String name ){

This. name = name;

}

Public int getPojoId (){

Return pojoId;

}

Public void setPojoId (int pojoId ){

This. pojoId = pojoId;

}

Public char [] getOptions (){

Return options;

}

Public void setOptions (char [] options ){

This. options = options;

}

Public String getFunc1 (){

Return func1;

}

Public void setFunc1 (String func1 ){

This. func1 = func1;

}

Public JSONFunction getFunc2 (){

Return func2;

}

Public void setFunc2 (JSONFunction func2 ){

This. func2 = func2;

}

}

3. JSON string --> Object

The code is as follows: Copy code

Public static void test3 (){

System. out. println ("------------ JSON string --> Object ----------");

String json1 = "{name =" json ", bool: true, int: 1, double: 2.2, func: function (a) {return a ;}, array: [1, 2]} ";

JSONObject jsonObject1 = JSONObject. fromObject (json1 );

Object bean1 = JSONObject. toBean (jsonObject1 );

System. out. println (bean1 );

// Net. sf. ezmorph. bean. MorphDynaBean @ 10dd1f7 [

// {Double = 2.2, func = function (a) {return a;}, int = 1, name = json, bool = true, array = [1, 2]}

//]

String json2 = "{bool: true, integer: 1, string:" json "}";

JSONObject jsonObject2 = JSONObject. fromObject (json2 );

BeanA bean2 = (BeanA) JSONObject. toBean (jsonObject2, BeanA. class );

System. out. println (bean2 );

// BeanA {bool = true, integer = 1, string = 'json '}

}

Public class BeanA {

Private boolean bool;

Private Integer;

Private String string;

Public boolean isBool (){

Return bool;

}

Public void setBool (boolean bool ){

This. bool = bool;

}

Public Integer getInteger (){

Return integer;

}

Public void setInteger (Integer integer ){

This. integer = integer;

}

Public String getString (){

Return string;

 

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.