Automatically generate javabean classes based on json strings

Still worried about variables in the document, or dealing with complex JSON structures?

One wrong type can lead to crashes, and a typo in a variable name can cause unresolved errors.

Introducing our code tool—automatically generate JavaBean classes with just one click!

This series of tools is designed to handle those tedious tasks for you, 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.

2. (You can get the JSON data from the example in the document, or by running some interface call code to print it out first.)

2. Run the JsonUtils class in the tool. The main method will automatically parse the JSON.

3. Open the JsonBean.java file, refresh, and copy the generated JavaBean class directly into your project.

Supplement

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

To generate getters and setters, right-click on Source and select Generate Getter and Setter.

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

That’s the end of the tool introduction. The underlying principle will be explained next.

Principle

This is a standard Java Project that uses the Gson library to parse JSON strings and build a tree structure.

Then, it uses File I/O streams to write the JavaBean structure into a file according to the parsed data.

All the source code is available in the project, with clear comments—almost every two lines of code have one line of comment.

Here is the core class JsonUtils 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 /Json/JsonString.txt 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 [], it will default to Object type.

*/

public static void parseJson2Java() {

// Read JSON string

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

// Parse JSON structure

JsonParser parser = new JsonParser();

JsonElement element = parser.parse(string);

JsonObject jo = element.getAsJsonObject();

List jsonBeanTree = getJsonBeanTree(jo);

// Generate JavaBean content

String javaBeanStr = createJavaBean(jsonBeanTree);

// Write to file

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

}

/**

* Create 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();

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(";");

}

}

sb.append("}");

return sb.toString();

}

Air Blown Fiber Optic Cable

air blown micro cable,air blown fiber optic cable,air blown fiber,air blown fiber cable

Guangzhou Jiqian Fiber Optic Cable Co.,ltd , https://www.jqopticcable.com