用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

从JSON数据创建Java类

2015-02-23 作者: java源代码大全举报

[java]代码库

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来处理的//源代码片段来自云代码http://yuncode.net
			


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...