You are still worrying about the variable in the document, the json with complicated structure~
Wrong one type, then various crashes
Wrong to write a variable name can not be resolved
The code tool is coming~
Automatically generate javabean class, just one key~
This series of code tools is dedicated to this, doing these boring tasks for you, giving you more time to study new poses.
Instructions
First import the code tool into Eclipse, the project is Java Project, pay attention to not Android Project
1. Copy the json string data to the jsonString.txt file in the project.
(json data can be obtained from the json data example in the document, or by running the interface call code to print the json string first)
2. Run the JsonUTIls class in the code tool, where the main function will run the json parsing main method.
3. Open the JsonBean.java file, refresh, copy and use the automatically generated javabean class directly.
supplement
If you need public modification variables, you can use ctrl+F to replace them all.
If you need to generate getter and setter methods, you can right click on Source->Generate Getter and Setter to set
The tool code is open source and can be modified as needed.
Ok, the tool ends here, and the code principle is introduced later.
principle
Project is a Java Project
Use the Gson tool to parse the string tree structure, and then use the File IO stream to write the bean structure to the file according to the requirements.
Visible source code in the project, conscience annotation, almost 2 lines of code 1 line comment
Here also posted the code of the core class JsonUTIls
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, and then call this method.
* 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 [] which cannot determine the type, the Object type will be used 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 collection
JsonParser parser = new JsonParser();
JsonElement element = parser.parse(string);
JsonObject jo = element.getAsJsonObject();
ListjsonBeanTree = getJsonBeanTree(jo);
/ / Use the obtained json structure collection to create the corresponding javabean file content
String javaBeanStr = createJavaBean(jsonBeanTree);
// Write the generated content to a file
FileUtils.writeString2File(javaBeanStr, new File("Json\\JsonBean.java"));
}
/**
* Create a corresponding javabean class string based on the parsed data
*
* @param jsonBeanTree parsed data collection
* @return generated javabean class string
*/
Private static String createJavaBean(ListjsonBeanTree) {
StringBuilder sb = new StringBuilder();
// Whether to include a custom subclass
Boolean hasCustomeClass = false;
ListcustomClassNames = new ArrayList();
Sb.append("public class JsonBeans {");
// Use iterator traversal because there is a remove operation during the loop
Iteratoriterator = jsonBeanTree.iterator();
While(iterator.hasNext()) {
Json2JavaElement j2j = iterator.next();
// Save the custom class name to the collection, note that the included ones are no longer added
If(j2j.getCustomClassName() != null && !customClassNames.contains(j2j.getCustomClassName())) {
customClassNames.add(j2j.getCustomClassName());
}
If(j2j.getParentJb() != null) {
/ / If there is a parent, it is a custom subclass, set the identifier to do no other operations
hasCustomeClass = true;
} else {
/ / If not a custom subclass, generate a variable declaration statement based on the type name and control object name
// private TextView tv_name;
Sb.append("private")
.append(getTypeName(j2j))
.append(" ")
.append(j2j.getName())
.append(";");
Barium titanate lead-free piezoelectric ceramics are important basic materials for the development of modern science and technology, which was widely used in the manufacture of ultrasonic transducers, underwater acoustic transducers, electroacoustic transducers, ceramic filters, ceramic transformers, ceramic frequency discriminators, high voltage generators, infrared detectors, surface acoustic wave devices, electro-optic devices, ignition and detonation devices, and piezoelectric gyroscope and so on.
Application: military, ocean, fishery, scientific research, mine detection, daily life and other fields.
Piezoelectric Ring,Lead Free Piezoelectric Ring,Piezo Electric Ring,Lead Free Piezo Rings
Zibo Yuhai Electronic Ceramic Co., Ltd. , https://www.yhpiezo.com