Using annotation auxiliary json-lib conversion JavaBean

Source: Internet
Author: User
Tags filter json tojson tostring

Overview

Today, a large number of WEB sites to apply AJAX technology, with less data communication, the server can be faster feedback user requests, and then through Javascript control, so that users have a better user experience. JSON is a lightweight data exchange language that is a subset of Javascript and has good readability and is often used for data exchange between clients and servers. Therefore, on the server side, it is often necessary to convert entity objects (JavaBean) to JSON-formatted data. This article describes how to use Json-lib to convert JavaBean to Json-formatted data, give a workaround, and use annotation to enhance the Json-lib's two features: one is to flexibly filter JavaBean attributes; the second is through JSONVALUEP Rocessor from defining how to convert JavaBean properties to JSON data.

JSON data format

JSON's data format is simple and readable, and it exists in two basic ways:

Name value pairs (Collection): Name and value used ': ' Separate, between name value pairs with ', ' delimited; the whole is enclosed in ' {} '. For example {name1:value1, name2:value2}

The ordered queue (array) of values: arrays, each value is separated by ', ', and the whole is enclosed in ' [] '. For example: [Value1, value2]

These two forms of organic combination form the JSON data.

Using Json-lib to convert JavaBean to Json data

Json-lib is a Java tool library that provides APIs to convert Javabean,map, Collection, and other objects to JSON data, or, conversely, JavaBean through JSON data.

Json-lib is easy to use, as long as you use the Jsonserializer Tojson method to convert any Java object to JSON objects, and then call the JSON object's ToString method to get the converted string. But there are some further issues that need to be solved by ourselves.

Listing 1. Using Jsonserializer

import net.sf.json.JSONSerializer;

List list = new ArrayList();
list.add( "first" );
list.add( "second" );
JSON json = JSONSerializer.toJSON( list );
System.out.println( json.toString() );
// prints ["first","second"]

class MyBean{
   private String name = "json";
   private int pojoId = 1;

   // getters & setters
   ...
}
json = JSONSerializer.toJSON( new MyBean() );
System.out.println( json.toString() );
// prints {"name":"json","pojoId":1}

Problem 1: Require selective extraction of attributes in JavaBean

The example in Listing 1 contains all the attributes in the JavaBean in the converted JSON data, but we often need to have a selective extraction of the specific attributes in the JavaBean. For example:

You need to filter out the properties of the circular reference, which Json-lib provides a cycledetectionstrategy to handle, but it's simpler to filter it out directly;

In different cases, only some of the attributes in the JavaBean are required: for example, the list interface only needs to display several important properties of the bean, while the detail interface needs to display more bean properties;

Different user rights limit the user can only get some attribute data;

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.