花了我三个多小时(java还需要努力啊);
只有我自己看得懂,哈哈!!!!
/**elbert.chenh寫於2010年1月5日晚
* 实现如下功能
* 1.将用户传递的个性化的jsonstr队列转换为标准化的统一的jsonstr队列
* 如队列中的元素{/"taskId/":/"t01/",/"sceneId/":/"s01/",/"caseId/":/"cs01/",/"SYNOPSIS/":/"p1/",/"id/":1,/"cpu/":/"cpu01/"}
* 转化为标准化元素{/"taskId/":/"t01/",/"sceneId/":/"s01/",/"caseId/":/"cs01/",/"param1/":/"p1/",/"id/":1,/"param10/":/"cpu01/"}
* 2.将队列中的jsonstr转换为对应的类实例
* 3.调用插入数据库函数将结果插入数据库中
* @param jstrCaseLog
*/
public void insertCaseLog(String jstrCaseLog)
{
List<Object> ls = com.alisoft.testplat.help.JsonFunc.fromJSON(
jstrCaseLog, ArrayList.class);
//取得数据库中所有的LogParam
TestCaseService tss = new TestCaseService();
List<LogParam> lp = tss.getLogParamList();
List<LinkedHashMap> lstmp = new ArrayList<LinkedHashMap>();
LinkedHashMap map = new LinkedHashMap();
for (int i = 0; i < ls.size(); i++) {
map = (LinkedHashMap) ls.get(i);
for (int j = 0; j < map.keySet().size(); j++) {
//使用Key去寻找是哪个Param,如果不是那么就返回自己
for(int k = 0; k < lp.size(); k++)
{
LogParam tmplp = lp.get(k);
//如果传入的参数别名=某条记录的参数别名,那么替换为该参数实际名称
if(map.keySet().toArray()[j].toString().toLowerCase().equals(tmplp.getParamAlias().toLowerCase()))
{
Object value = map.get(map.keySet().toArray()[j]);
map.remove(map.keySet().toArray()[j]);
map.put(tmplp.getParamName().toLowerCase(), value);
}
}
}
lstmp.add(map);
}
String tempstr = com.alisoft.testplat.help.JsonFunc.toJSON(lstmp);
List<Object> deslp = com.alisoft.testplat.help.JsonFunc.fromJSON(tempstr, ArrayList.class);
for(int index = 0; index < deslp.size(); index++)
{
String cp = getListNode(deslp,index);
CaseLog tlp = com.alisoft.testplat.help.JsonFunc.fromJSON(cp, CaseLog.class);
CaseLogDao.InsertCaseLog(tlp);
}
}
public String getListNode(List<Object> ls,int index)
{
StringBuilder desstr = new StringBuilder("{");
LinkedHashMap map = new LinkedHashMap();
map = (LinkedHashMap) ls.get(index);
for (int j = 0; j < map.keySet().size(); j++) {
//如果传入的参数别名=某条记录的参数别名,那么替换为该参数实际名臣
Object key = map.keySet().toArray()[j];
Object value = map.get(map.keySet().toArray()[j]);
if(j < (map.keySet().size()-1))
{
if (value != null)
{
desstr.append("/"").append(key.toString()).append("/":").append("/"").append(value.toString()).append("/"").append(",");
}
else
{
desstr.append("/"").append(key.toString()).append("/":").append("null").append(",");
}
}
else
{
if (value != null)
{
desstr.append("/"").append(key.toString()).append("/":").append("/"").append(value.toString()).append("/"").append("}");
}
else
{
desstr.append("/"").append(key.toString()).append("/":").append("null").append("}");
}
}
}
System.out.println(desstr.toString());
return desstr.toString();
}
public static String toJSON(Object obj) {
StringWriter writer = new StringWriter();
try {
mapper.writeValue(writer, obj);
} catch (JsonGenerationException e) {
throw new RuntimeException(e);
} catch (JsonMappingException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
return writer.toString();
}
public static <T> T fromJSON(String json, Class<T> clazz) {
ObjectMapper mapper = new ObjectMapper();
try {
return mapper.readValue(json, clazz);
} catch (JsonParseException e) {
throw new RuntimeException(e);
} catch (JsonMappingException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}