[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