Java Programmer's Good tool: Json-to-javabean

Source: Internet
Author: User

Introduce a tool I developed, Json-to-java. It can help Java programmers to generate corresponding JavaBean through JSON. This tool is especially useful when you need to invoke the JSON return format API.

function Introduction

We develop Java systems that often need to invoke APIs from other systems. The return formats for these APIs are often in JSON format. This gadget can help you convert JSON-formatted data into corresponding JavaBean. To simplify the work of manually copying and pasting JSON properties.

Who will benefit?

If you need to invoke a third-party API, you face the following scenario:
1, the API does not provide the Java SDK, map to the Java response format, to write their own
2, API provides the SDK, but the SDK is slightly heavy, do not want to toss

Instructions for use

To use this project immediately, please visit
http://jsontojava.sinaapp.com/
Go to the URL above, or download the code in git to open index.html. The page looks like this. An example is shown by default.

1, you can paste the JSON data in the left input box
2. The box on the right will show the corresponding JavaBean.
3. Then you can copy the contents of the right to your project.

Support Features automatic recognition of attribute values, Int,double,string,date,boolean, arrays, etc. can now be identified

For example:
1. The JSON format is "created_at": "2012-05-23T08:00:58Z" recognized as a date type and generates properties private Date createdAt
2. Array recognition

{   "Tags":[        {"Count":2416,"name":"The Little Prince"},        {"Count":1914,"name":"Fairy tale"},        {"Count":1185,"name":"Saint Exupery"},        {"Count":863,"name":"France"},        {"Count":647,"name":"Classic"},        {"Count":597,"name":"Foreign literature"},        {"Count":495,"name":"very impressed"},        {"Count":368,"name":"Fable"}        ]}

will be converted to

publicclass Example {   private List<Tags> tags;    publicvoidsetTags(List<Tags> tags) {        this.tags = tags;    }    publicgetTags() {        return tags;    }}
automatically converts a lowercase named property to a hump name

The attribute name of the CREATE_AT will be converted to Createdat and annotated

@JsonProperty("created_at")private Date createdAt;
If the JSON is an array , the first value in the arrays is automatically recognized

As the following example, the JSON is judged to be an array and then parsed by the first value of the JavaBean

[        {          "ID":1,          "username":"John_smith",          "Email":"[email protected]"        },        {          "ID":2,          "username":"Jack_smith",          "Email":"[email protected]",          "name":"Jack Smith."        }]
supports one-click Copy to Clipboard

When the JSON parsing error occurs, the line with the error is displayed

the ability to automatically recognize attribute values as custom classes

Values such as the images property in, its value is not the base type (string,int, etc.), but the custom data structure

{    "id":"1003078",    "images":{        "small":"http:\/\/img1.douban.com\/spic\/s1001902.jpg",        "large":"http:\/\/img1.douban.com\/lpic\/s1001902.jpg",        "medium":"http:\/\/img1.douban.com\/mpic\/s1001902.jpg"     }}

For this type of custom class, the following processing is done
1. The type name (class name) of the property, which is obtained by converting the property name. For example, the type of the property is Imagas, which is to capitalize the attribute name images to the first letter.

publicclass Example {   private String id;   private Images images;}

2. The JavaBean of the custom class is generated.

Other Notes
    • The code has been placed in the Github,github address Https://github.com/giantray/text-to-javabean
    • One-click Upload function, you need to deploy this project on the server to use

Java Programmer's Good tool: Json-to-javabean

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.