4
回答
如何从JSON数据创建Java类
华为云实践训练营,热门技术免费实践!>>>   

Hi,

   经常在写API文档,需要根据API文档中定义的JSON对象在项目中编写对应的JAVA对象,由于有的JSON属性很多,又是多层嵌套所以一个个手写JAVA的属性很麻烦,想问大家如何快速根据JSON生成JAVA对象?

想要的东西如此帖,不过在线的那个工具已废,登不上了。

举报
Kent_Chen
发帖于3年前 4回/1K+阅
共有4个答案 最后回答: 3周前
package com.test;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;
public class Test {
public final static String FILE_PATH = "d:";
public final static String CLASS_HEAD_NAME ="public class ";
public final static String FUHAO_1 = " {";
public final static String HUAN_HANG = ";\r\n";
public static void main(String[] args) {
	JsonFactory f = new JsonFactory();
	List<ClsFile> sbList = new ArrayList<ClsFile>();
	try {
		JsonParser jp = f.createJsonParser("{\"name\":\"admin\",\"age\":10,\"data\":{\"max\":10,\"min\":5}}");
		StringBuilder sbBuilder  = null;
		while(jp.nextToken() !=JsonToken.END_OBJECT)
		{
			JsonToken tokenName =  jp.getCurrentToken();
			String curName = jp.getCurrentName();
			if(curName != null)
			{
				String name = tokenName.toString();
				if(name.equals("START_OBJECT"))
				{
					creatFieldName(sbBuilder,curName,curName);
					ClsFile clsFile = createClsHead(sbList,curName);
					sbBuilder = clsFile.getContent();
				}
				else
				{
					if(!name.equals("FIELD_NAME"))
					{
						if(name.equals(JsonToken.VALUE_NUMBER_INT.toString()))
						{
							creatFieldName(sbBuilder,jp.getCurrentName(),"int");
						}
						if(name.equals(JsonToken.VALUE_STRING.toString()))
						{
							creatFieldName(sbBuilder,jp.getCurrentName(),"String");
						}
					}
				}
			}
			else
			{
				//第一个类名
				curName = "clsName";
				ClsFile clsFile = createClsHead(sbList,curName);
				sbBuilder = clsFile.getContent();
			}
		}
		for(ClsFile cf: sbList)
		{
			writeClassFile(cf.getClassName(),cf.getContent().toString());
		}
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	}

	public final static ClsFile createClsHead(List<ClsFile> sbList,String className)
	{
		ClsFile cf = new ClsFile();
		cf.setClassName(className);
		cf.setContent(new StringBuilder(CLASS_HEAD_NAME+cf.getClassName()+FUHAO_1+HUAN_HANG));
		sbList.add(cf);
		return cf;
	}
	
	public final static void creatFieldName(StringBuilder fieldNameBuilder,String fieldName,String fieldType)
	{
		fieldNameBuilder.append("private "+fieldType +" "+fieldName+HUAN_HANG);
	}

	/**
	 * 创建类文件
	 * @param className
	 * @param content
	 */
	public final static void writeClassFile(String className,String content)
	{
		writeFile(FILE_PATH,className+".java",content);
	}
	
	/**
	 * 写入文件内容
	 * @param path
	 * @param name
	 * @param content
	 */
	public final static void writeFile(String path,String name,String content)
	{
		File file = new File(path+"/"+name);
		try {
			file.createNewFile();
			FileOutputStream os = new FileOutputStream(file);
			OutputStreamWriter osWriter = new OutputStreamWriter(os);
			BufferedWriter buffWriter = new BufferedWriter(osWriter);
			buffWriter.write(content);
			buffWriter.flush();
			buffWriter.close();
			osWriter.close();
			os.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
class ClsFile
{
	private String className;
	private StringBuilder content;
	public String getClassName() {
		return className;
	}
	public void setClassName(String className) {
		this.className = className;
	}
	public StringBuilder getContent() {
		return content;
	}
	public void setContent(StringBuilder content) {
		this.content = content;
	}
}

大概写了下  思路是差不多的  使用jackson来处理的



顶部