日期:2014-05-20 浏览次数:20697 次
private boolean getResult(String flag,JSONObject jsonObject) throws Exception{
if(null != jsonObject && !jsonObject.isEmpty()){
JSONArray jsonArray = new JSONArray();
Iterator<Object> i = jsonObject.keys();
while(i.hasNext()){
String key = i.next().toString();
try {
JSONObject value = jsonObject.getJSONObject(key);
Json2Bool j2b = new Json2Bool();
if(j2b.getResult(key,value)){
jsonArray.add("true");
}else{
jsonArray.add("false");
}
} catch (Exception e) {
JSONArray value = jsonObject.getJSONArray(key);
jsonArray.add(str2Bool(key,value)+"");
}
}
return str2Bool(flag,jsonArray);
}else{
System.out.println("error:格式错误");
return false;
}
}
private boolean str2Bool(String flag,JSONArray list) throws Exception{
if(null == flag
------解决方案--------------------
null == list
------解决方案--------------------
list.size() == 0){
throw new BoolException();
}else{
if("or".equalsIgnoreCase(flag)){
boolean result = false;
for(int i=0;i<list.size();i++){
String value = list.get(i).toString();
if(str2Bool(value)){
result = true;
break;
}else{
continue;
}
}
return result;
}else if("and".equalsIgnoreCase(flag)){
boolean result = true;
for(int i=0;i<list.size();i++){
String value = list.get(i).toString();
if(str2Bool(value)){
continue;
}else{
result = false;
break;
}
}
return result;
}else{
throw new BoolException();
}
}
}
private boolean str2Bool(String str) throws Exception{
if(null == str){
throw new BoolException();
}
if("true".equals(str)){
return true;
}else if("false".equals(str)){
return false;
}else{
throw new BoolException();
}
}