Using Jsonobject to generate JSON in Java

Source: Internet
Author: User
Tags comparable package json tojson

JSON is a lightweight data interchange format that is very common in today's web development. In the absence of convenient tools, we may use the form of a spelling string to generate a JSON array, today we use a Json-lib.jar package for us to implement the function of generating JSON.

The necessary packages are:

Commons-httpclient-3.1.jar

Commons-lang-2.4.jar

Commons-logging-1.1.1.jar

Json-lib-2.4-jdk15.jar

Ezmorph-1.0.6.jar

Commons-collections-3.2.1.jar

1. The bean is converted to JSON

User U = new user (); U.setage (22);

U.setusername ("HZUCMJ"); U.setenabled (TRUE);

Jsonobject json = jsonobject.fromobject (u);

System.out.println (Json.tostring ());

The result is:

{"Enabled": True, "username": "HZUCMJ", "Age": 22}

2. List is converted to JSON

User U1 = new user (); U1.setage (22);

U1.setusername ("HZUCMJ");

U1.setenabled (TRUE);

User U2 = new user ();

U2.setage (20); U2.setusername ("CTF");

U2.setenabled (TRUE);

list<object> list = new arraylist<object> ();

List.add (U1);

List.add (U2);

Jsonarray json = jsonarray.fromobject (list);

System.out.println (Json.tostring ());

The result is:

[{"Enabled": False, "username": "CTF", "Age": 20},{"Enabled": False, "username": "", "Age": 0}]

3. Map to JSON

hashmap<string, comparable> map = new hashmap<string, comparable> ();

Map.put ("name", "HZUCMJ"); Map.put ("Age", 22);

Jsonobject json = jsonobject.fromobject (list);

System.out.println (Json.tostring ());

The result is:

{"Name": "HZUCMJ", "Age": 22}

Based on the Json-lib.jar packet Jsonobject-java common four kinds of usage

Based on Json-lib.jar package JSON

Instance Program 1.

Jsonobject to Dynabean String json = "{name=\" json\ ", bool:true,int:1,double:2.2}";

Jsonobject Jsonobject = Jsonobject.fromobject (JSON);

Abstract notation:

Dynabean bean = (dynabean) Jsonserializer.tojava (jsonobject);

Object bean = Jsonobject.tobean (jsonobject);

Object bean1 = Jsonserializer.tojava (Jsonobject);

Assertequals (Jsonobject.get ("name"), Propertyutils.getproperty (Bean, "name"));

Assertequals (Jsonobject.get ("bool"), Propertyutils.getproperty (Bean, "bool"));

Assertequals (Jsonobject.get ("int"), Propertyutils.getproperty (Bean, "int"));

Assertequals (Jsonobject.get ("Double"), Propertyutils.getproperty (Bean, "double"));

2.JSONObject to JavaBean String json = "{name:\" zhangsan\ ", age:25,hight:1.72,sex:true}";

Jsonobject Jsonobject = Jsonobject.fromobject (JSON);

UserBean bean = (UserBean) Jsonobject.tobean (Jsonobject, Userbean.class);

System.out.println (Jsonobject);

Theoretically, this would be possible, but when there was an anomaly caused by:java.lang.nosuchmethodexception:com.json.json$userbean.<init> ()

3.JSONArray to List String json = "[\" first\ ", \" second\ "]";

Jsonarray Jsonarray = (jsonarray) Jsonserializer.tojson (JSON);

List output = (list) Jsonserializer.tojava (Jsonarray);

4.JSONArray to array String json = "[\" first\ ", \" second\ "]";

Jsonarray Jsonarray = (jsonarray) Jsonserializer.tojson (JSON);

Jsonconfig jsonconfig = new Jsonconfig ();

Jsonconfig.setarraymode (Jsonconfig.mode_object_array);

object[] output = (object[]) Jsonserializer.tojava (Jsonarray, jsonconfig);

Object[] expected = new object[] {"First", "second"};

Arrayassertions.assertequals (expected, output);

Using Jsonobject to generate JSON in Java

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.