Still worried about the variables in your document? Dealing with complex JSON structures can be a real pain.
A single type mismatch can lead to crashes, and even a typo in a variable name can cause errors that are hard to trace.
That's why we're introducing a powerful code tool designed to make your life easier.
This tool automatically generates JavaBean classes with just one click, saving you from the tedious work of manually creating classes.
It's part of a series of code tools aimed at handling repetitive tasks, so you can focus on learning new skills and improving your coding efficiency.
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 located in the project.
2. The JSON data can be obtained from the example provided in the document or by running an interface call to print the JSON string first.
3. Run the JsonUtils
class in the code tool. The main method will parse the JSON and generate the JavaBean class.
4. Open the JsonBean.java
file, refresh it, and copy the generated JavaBean class for immediate use.
Supplement
If you need to change variable access modifiers (like making them public), you can use Ctrl + F
to replace all occurrences.
To generate getter and setter methods, right-click on the source code and select Generate Getter and Setter
.
The tool is open-source, so you can modify it according to your needs.
This tool is just the beginning. In the next section, I'll explain how it works under the hood.
Principle
The project is a standard Java Project. It uses the Gson library to parse the JSON structure and then writes the corresponding JavaBean class using File IO.
All the source code is visible within the project, with clear comments and minimal lines of code. Most of the code is straightforward, with each line accompanied by a comment.
I've also included the core class JsonUtils
below 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 is a null or empty collection in the JSON string like [], the Object type will be used uniformly.
*/
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
String javaBeanStr = createJavaBean(jsonBeanTree);
FileUtils.writeString2File(javaBeanStr, new File("Json\\JsonBean.java"));
}
private static String createJavaBean(List
StringBuilder sb = new StringBuilder();
Boolean hasCustomClass = false;
List
sb.append("public class JsonBeans {");
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();
}
}
understanding mining,outdoor mining,Mining Fiber Cable
Guangzhou Jiqian Fiber Optic Cable Co.,ltd , https://www.jqopticcable.com