packagecom.sunline.nfs.process;importjava.io.BufferedReader;importjava.io.File;importjava.io.FileReader;importjava.io.IOException;importjava.io.StringWriter;importjava.util.LinkedHashMap;importjava.util.Map;importjava.util.Properties;importnet.sf.json.JSONArray;importnet.sf.json.JSONObject;importorg.apache.velocity.Template;importorg.apache.velocity.VelocityContext;importorg.apache.velocity.app.Velocity;importorg.apache.velocity.app.VelocityEngine;importorg.springframework.core.io.ClassPathResource;importorg.springframework.core.io.Resource;public classvelocityEngine {static Map jsonMap = new LinkedHashMap();public static Map recursinMap(Mapmap){
net.sf.json.JSONObject jObject= newnet.sf.json.JSONObject();for(String key:map.keySet()){
String str=map.get(key).toString();if(str.startsWith("{")){/*com.alibaba.fastjson.JSONObject jObject = JSON.parseObject(str);*/Map map2 =jObject.fromObject(str);if(!map.toString().endsWith(map2.toString()+"}")){int i = map.toString().indexOf(map2.toString())+1;
String value="{" + map.toString().substring((i+map2.toString().length()),map.toString().length());
map2.put(key, value);
}returnrecursinMap(map2);
}else{if(str.startsWith("[")){
JSONArray tmpMaps=JSONArray.fromObject(str);
System.out.println("key="+key+" tmpMaps"+tmpMaps);
jsonMap.put(key, tmpMaps);/*for(int i=0;i
JSONObject job = tmpMaps.getJSONObject(i);
Map jobMap = JSONObject.fromObject(job.toString());
for(String key2:jobMap.keySet())
jsonMap.put(key+i+key2, jobMap.get(key2));
}*/}else{
jsonMap.put(key, str);
}
}
}returnjsonMap;
}
@SuppressWarnings("null")public static void main(String[] args) throwsIOException{
String filename= "ObtainAppNo.txt";
String json=readJson(filename);
net.sf.json.JSONObject jObject= newnet.sf.json.JSONObject();
Map map2 =jObject.fromObject(json);
Map jsonMap =recursinMap(map2);
System.out.println("-----" +jsonMap);//初始化参数
Properties properties=newProperties();//设置velocity资源加载方式为class
properties.setProperty("resource.loader", "class");//设置velocity资源加载方式为file时的处理类
properties.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");//实例化一个VelocityEngine对象
VelocityEngine velocityEngine=newVelocityEngine(properties);//实例化一个VelocityContext
VelocityContext context=newVelocityContext();for(Object key :jsonMap.keySet()){
context.put((String)key, jsonMap.get(key));
}//实例化一个StringWriter
StringWriter writer=newStringWriter();//假若在com.velocity.test包下有一个hello.vm文件,那么加载路径为com/velocity/test/hello.vm//String json2 = readJson("2.txt");//velocityEngine.mergeTemplate(json2, "gbk", context, writer);//这种方式是正确的
velocityEngine.mergeTemplate("2.txt", "gbk", context, writer);
String str=writer.toString();
str= str.replaceAll("\\s*", "");
System.out.println("-----" +str);
}public staticString readJson(String filename){
Resource resource=newClassPathResource(filename);
File file;try{
file=resource.getFile();
BufferedReader reader= new BufferedReader(newFileReader(file));
String temp=null;
String str="";while((temp=reader.readLine())!=null){
str= str +temp;
}
str=str.replace(" ", "");returnstr;
}catch(IOException e) {
e.printStackTrace();
}return null;
}
}