Automatically generate javabean classes based on json strings

You're still stressed about variables in the document, and dealing with complex JSON structures can be really frustrating.

A wrong data type can lead to all sorts of crashes and errors.

Mistyping a variable name can result in something that just doesn't resolve at all.

That's where our code tool comes in handy!

It automatically generates JavaBean classes with just one click.

This series of tools is designed to take care of those repetitive and tedious tasks, so you can focus more on learning new techniques and improving your skills.

Instructions

First, import the code tool into Eclipse as a Java Project—not an Android Project.

1. Copy the JSON string data into the jsonString.txt file inside your project.

2. The JSON data can be obtained from the example provided in the documentation or by running some interface call code to print the JSON string first.

3. Run the JsonUtils class in the code tool. The main method will handle the JSON parsing process.

4. Open the JsonBean.java file, refresh it, and then copy the generated JavaBean class directly into your project for use.

Supplement

If you need to make variables public, you can use Ctrl + F to replace them all at once.

If you want to generate getter and setter methods, right-click on Source and choose Generate Getter and Setter.

The tool’s source code is open-source, so you can modify it according to your needs.

Alright, that’s the tool. We’ll explain the inner workings in the next section.

Principle

The project is a standard Java Project.

We use the Gson library to parse the JSON string and analyze its structure. Then, we use File I/O streams to write the JavaBean class into a file based on the parsed data.

All the source code is visible within the project, with clear comments—almost every line has a comment to help you understand what’s going on.

I’ve also shared the core class JsonUtils here for reference:

package utils;

import java.io.File;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

import java.util.Map.Entry;

import com.google.gson.JsonArray;

import com.google.gson.JsonElement;

import com.google.gson.JsonObject;

import com.google.gson.JsonParser;

import com.google.gson.JsonPrimitive;

import entity.ArrayType;

import entity.Json2JavaElement;

public class JsonUtils {

public static void main(String[] args) {

parseJson2Java();

}

/**

* Convert JSON string to corresponding JavaBean

* Usage:

* Copy the JSON string to the /Json/JsonString.txt file in this project, then call this method.

* It will generate a corresponding JavaBean class in /Json/JsonBean.java in this project.

* Note:

* If there's a null or empty collection like [] in the JSON string that cannot determine the type, it will default to Object.

*/

public static void parseJson2Java() {

String string = FileUtils.readToString(new File("Json\\JsonString.txt"), "UTF-8");

JsonParser parser = new JsonParser();

JsonElement element = parser.parse(string);

JsonObject jo = element.getAsJsonObject();

List jsonBeanTree = getJsonBeanTree(jo);

String javaBeanStr = createJavaBean(jsonBeanTree);

FileUtils.writeString2File(javaBeanStr, new File("Json\\JsonBean.java"));

}

/**

* Create a JavaBean class string based on the parsed data

* @param jsonBeanTree Parsed data collection

* @return Generated JavaBean class string

*/

private static String createJavaBean(List jsonBeanTree) {

StringBuilder sb = new StringBuilder();

boolean hasCustomClass = false;

List customClassNames = new ArrayList<>();

sb.append("public class JsonBeans {");

Iterator iterator = jsonBeanTree.iterator();

while (iterator.hasNext()) {

Json2JavaElement j2j = iterator.next();

if (j2j.getCustomClassName() != null && !customClassNames.contains(j2j.getCustomClassName())) {

customClassNames.add(j2j.getCustomClassName());

}

if (j2j.getParentJb() != null) {

hasCustomClass = true;

} else {

sb.append("private ").append(getTypeName(j2j)).append(" ").append(j2j.getName()).append(";");

}

}

// ... (rest of the code continues)

Dumbwaiter Elevator

Food Elevator,Dumbwaiter Elevator,Food Conveying Elevator,Food Lift

ZHONG HAN INTERNATIONAL TRADE CO., LTD , https://www.cck-ht.com

Posted on