Automatically generate javabean classes based on json strings

You're still worried about variables in the document, and the JSON with a complicated structure?

One wrong type can lead to all sorts of crashes.

A typo in a variable name might make it impossible to resolve.

Now, here comes the code tool to help you out!

It automatically generates JavaBean classes with just one key press.

This series of code tools is designed for you, handling those tedious tasks so you can focus more on learning new techniques.

Instructions

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

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

(You can get the JSON data from the example in the document or by running interface call code to print the JSON string first.)

2. Run the JsonUtils class in the code tool. The main function will run the JSON parsing method.

3. Open the JsonBean.java file, refresh it, and directly copy and use the automatically generated JavaBean class.

Supplement

If you need to modify variables to be 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 > Generate Getter and Setter to set them up.

The tool’s code is open source and can be modified as needed.

That's it for the tool. We’ll explain the inner workings later.

Principle

The project is a standard Java Project.

We use the Gson library to parse the JSON string and its tree structure, then write the JavaBean structure to a file using File IO streams.

All the source code is visible in the project, with clear annotations — almost every line has a comment.

I’ve also shared the core class JsonUtils below.

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.

* Note:

* If there is a null or empty array like [] that cannot determine the type, it will use Object type uniformly.

*/

Public static void parseJson2Java() {

// Read JSON string

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

// Parse to get the entire JSON structure

JsonParser parser = new JsonParser();

JsonElement element = parser.parse(string);

JsonObject jo = element.getAsJsonObject();

List jsonBeanTree = getJsonBeanTree(jo);

// Generate JavaBean class content based on parsed data

String javaBeanStr = createJavaBean(jsonBeanTree);

// Write the generated content to a file

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

}

/**

* Create a JavaBean class string based on parsed data

* @param jsonBeanTree Parsed data collection

* @return Generated JavaBean class string

*/

Private static String createJavaBean(List jsonBeanTree) {

StringBuilder sb = new StringBuilder();

// Whether to include custom subclass

Boolean hasCustomClass = false;

List customClassNames = new ArrayList<>();

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

// Use iterator because we may remove elements during iteration

Iterator iterator = jsonBeanTree.iterator();

While (iterator.hasNext()) {

Json2JavaElement j2j = iterator.next();

// Save custom class name if exists

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

customClassNames.add(j2j.getCustomClassName());

}

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

// If it has a parent, it's a custom subclass

hasCustomClass = true;

} else {

// Otherwise, generate a variable declaration

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

}

}

sb.append("}");

return sb.toString();

}

...

Dumbwaiter Elevator

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

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

Posted on