我正在研究调查应用程序并从服务器获取数据。我在将String转换为布尔条件时遇到问题,布尔条件是String格式。 这是条件。我在网上搜索过很多但没找到解决办法。 我已经阅读了一些与之相关的帖子,但没有帮助。
http://www.java-examples.com/convert-java-string-object-boolean-object http://beginnersbook.com/2014/07/convert-string-to-boolean-primitive-in-java/ http://www.tutorialspoint.com/java/lang/boolean_parseboolean.htm
String s = "{hidSection=if(((Q4a.NAOK==\"5\" && Q4b.NAOK==\"7\")|| (Q4a.NAOK==\"6\" && Q4b.NAOK==\"6\") || (Q4a.NAOK==\"6\" && Q4b.NAOK==\"7\")|| \n" +
" (Q4a.NAOK==\"7\" && Q4b.NAOK==\"5\")|| (Q4a.NAOK==\"7\" && Q4b.NAOK==\"6\") || (Q4a.NAOK==\"7\" && Q4b.NAOK==\"7\")|| \n" +
" (Q4a.NAOK==\"8\" && Q4b.NAOK==\"7\")|| (Q4a.NAOK==\"12\" && Q4b.NAOK==\"6\")|| (Q4a.NAOK==\"12\" && Q4b.NAOK==\"7\"))\n" +
" ,1,if(((Q4a.NAOK==\"4\" && Q4b.NAOK==\"6\") || (Q4a.NAOK==\"4\" && Q4b.NAOK==\"7\") || (Q4a.NAOK==\"5\" && Q4b.NAOK==\"5\")||\n" +
" (Q4a.NAOK==\"5\" && Q4b.NAOK==\"6\")|| (Q4a.NAOK==\"7\" && Q4b.NAOK==\"3\") || (Q4a.NAOK==\"6\" && Q4b.NAOK==\"5\")|| \n" +
" (Q4a.NAOK==\"7\" && Q4b.NAOK==\"4\")|| (Q4a.NAOK==\"8\" && Q4b.NAOK==\"6\")|| (Q4a.NAOK==\"10\" && Q4b.NAOK==\"7\")|| \n" +
" (Q4a.NAOK==\"11\" && Q4b.NAOK==\"6\")|| (Q4a.NAOK==\"11\" && Q4b.NAOK==\"7\")|| (Q4a.NAOK==\"12\" && Q4b.NAOK==\"5\"))\n" +
" ,2,if(((Q4a.NAOK==\"4\" && Q4b.NAOK==\"5\") || (Q4a.NAOK==\"5\" && Q4b.NAOK==\"4\")|| (Q4a.NAOK==\"6\" && Q4b.NAOK==\"4\")|| \n" +
" (Q4a.NAOK==\"7\" && Q4b.NAOK==\"1\")|| (Q4a.NAOK==\"7\" && Q4b.NAOK==\"2\")|| (Q4a.NAOK==\"8\" && Q4b.NAOK==\"5\")|| \n" +
" (Q4a.NAOK==\"9\" && Q4b.NAOK==\"6\")|| (Q4a.NAOK==\"9\" && Q4b.NAOK==\"7\")|| (Q4a.NAOK==\"10\" && Q4b.NAOK==\"6\")|| \n" +
" (Q4a.NAOK==\"11\" && Q4b.NAOK==\"5\")|| (Q4a.NAOK==\"12\" && Q4b.NAOK==\"1\")|| (Q4a.NAOK==\"12\" && Q4b.NAOK==\"2\")|| \n" +
" (Q4a.NAOK==\"12\" && Q4b.NAOK==\"3\")|| (Q4a.NAOK==\"12\" && Q4b.NAOK==\"4\"))\n" +
" ,3,if(((Q4a.NAOK==\"2\" && Q4b.NAOK==\"6\") || (Q4a.NAOK==\"2\" && Q4b.NAOK==\"7\")|| (Q4a.NAOK==\"3\" && Q4b.NAOK==\"6\")|| \n" +
" (Q4a.NAOK==\"3\" && Q4b.NAOK==\"7\")|| (Q4a.NAOK==\"4\" && Q4b.NAOK==\"4\")|| (Q4a.NAOK==\"5\" && Q4b.NAOK==\"3\")|| \n" +
" (Q4a.NAOK==\"6\" && Q4b.NAOK==\"2\")|| (Q4a.NAOK==\"6\" && Q4b.NAOK==\"3\")|| (Q4a.NAOK==\"8\" && Q4b.NAOK==\"4\")|| \n" +
" (Q4a.NAOK==\"9\" && Q4b.NAOK==\"5\")|| (Q4a.NAOK==\"10\" && Q4b.NAOK==\"5\")|| (Q4a.NAOK==\"11\" && Q4b.NAOK==\"4\"))\n" +
" ,4,if(((Q4a.NAOK==\"2\" && Q4b.NAOK==\"4\") || (Q4a.NAOK==\"2\" && Q4b.NAOK==\"5\")|| (Q4a.NAOK==\"3\" && Q4b.NAOK==\"4\")|| (Q4a.NAOK==\"3\" && Q4b.NAOK==\"5\")|| \n" +
" (Q4a.NAOK==\"4\" && Q4b.NAOK==\"3\")|| (Q4a.NAOK==\"5\" && Q4b.NAOK==\"2\")|| (Q4a.NAOK==\"6\" && Q4b.NAOK==\"1\")|| \n" +
" (Q4a.NAOK==\"9\" && Q4b.NAOK==\"4\")|| (Q4a.NAOK==\"10\" && Q4b.NAOK==\"3\")|| (Q4a.NAOK==\"10\" && Q4b.NAOK==\"4\")|| \n" +
" (Q4a.NAOK==\"11\" && Q4b.NAOK==\"1\")|| (Q4a.NAOK==\"11\" && Q4b.NAOK==\"2\")|| (Q4a.NAOK==\"11\" && Q4b.NAOK==\"3\"))\n" +
" ,5,if(((Q4a.NAOK==\"1\" && Q4b.NAOK==\"4\") || (Q4a.NAOK==\"1\" && Q4b.NAOK==\"5\")|| (Q4a.NAOK==\"1\" && Q4b.NAOK==\"6\")|| \n" +
" (Q4a.NAOK==\"1\" && Q4b.NAOK==\"7\")|| (Q4a.NAOK==\"2\" && Q4b.NAOK==\"3\")|| (Q4a.NAOK==\"3\" && Q4b.NAOK==\"2\")|| \n" +
" (Q4a.NAOK==\"3\" && Q4b.NAOK==\"3\")|| (Q4a.NAOK==\"4\" && Q4b.NAOK==\"1\")|| (Q4a.NAOK==\"4\" && Q4b.NAOK==\"2\")|| \n" +
" (Q4a.NAOK==\"5\" && Q4b.NAOK==\"1\")|| (Q4a.NAOK==\"8\" && Q4b.NAOK==\"1\")|| (Q4a.NAOK==\"8\" && Q4b.NAOK==\"2\")|| \n" +
" (Q4a.NAOK==\"8\" && Q4b.NAOK==\"3\")|| (Q4a.NAOK==\"9\" && Q4b.NAOK==\"1\")|| (Q4a.NAOK==\"9\" && Q4b.NAOK==\"2\")|| \n" +
" (Q4a.NAOK==\"9\" && Q4b.NAOK==\"3\")|| (Q4a.NAOK==\"10\" && Q4b.NAOK==\"1\")|| (Q4a.NAOK==\"10\" && Q4b.NAOK==\"2\"))\n" +
" ,6,if(((Q4a.NAOK==\"1\" && Q4b.NAOK==\"3\") || (Q4a.NAOK==\"2\" && Q4b.NAOK==\"2\"))\n" +
" ,7,if(((Q4a.NAOK==\"1\" && Q4b.NAOK==\"1\") || (Q4a.NAOK==\"1\" && Q4b.NAOK==\"2\") || (Q4a.NAOK==\"2\" && Q4b.NAOK==\"1\") || \n" +
" (Q4a.NAOK==\"3\" && Q4b.NAOK==\"1\"))\n" +
" ,8,\"\"))))))))}";
我跟着link。但是没有帮助我。如何将字符串转换为布尔条件?
答案 0 :(得分:1)
从服务器返回代码非常可怕。无论如何,我给你的代码做了一些步骤:
if
开头,以数字结尾。将它们添加到数组中。(Q4a.NAOK==\"7\" && Q4b.NAOK==\"5\")
(答案1和答案2)。 private void doStuff() {
int answer1 = 2;
int answer2 = 1;
String s = "{hidSection=if(((Q4a.NAOK==\"5\" && Q4b.NAOK==\"7\")|| (Q4a.NAOK==\"6\" && Q4b.NAOK==\"6\") || (Q4a.NAOK==\"6\" && Q4b.NAOK==\"7\")|| \n" +
" (Q4a.NAOK==\"7\" && Q4b.NAOK==\"5\")|| (Q4a.NAOK==\"7\" && Q4b.NAOK==\"6\") || (Q4a.NAOK==\"7\" && Q4b.NAOK==\"7\")|| \n" +
" (Q4a.NAOK==\"8\" && Q4b.NAOK==\"7\")|| (Q4a.NAOK==\"12\" && Q4b.NAOK==\"6\")|| (Q4a.NAOK==\"12\" && Q4b.NAOK==\"7\"))\n" +
" ,1,if(((Q4a.NAOK==\"4\" && Q4b.NAOK==\"6\") || (Q4a.NAOK==\"4\" && Q4b.NAOK==\"7\") || (Q4a.NAOK==\"5\" && Q4b.NAOK==\"5\")||\n" +
" (Q4a.NAOK==\"5\" && Q4b.NAOK==\"6\")|| (Q4a.NAOK==\"7\" && Q4b.NAOK==\"3\") || (Q4a.NAOK==\"6\" && Q4b.NAOK==\"5\")|| \n" +
" (Q4a.NAOK==\"7\" && Q4b.NAOK==\"4\")|| (Q4a.NAOK==\"8\" && Q4b.NAOK==\"6\")|| (Q4a.NAOK==\"10\" && Q4b.NAOK==\"7\")|| \n" +
" (Q4a.NAOK==\"11\" && Q4b.NAOK==\"6\")|| (Q4a.NAOK==\"11\" && Q4b.NAOK==\"7\")|| (Q4a.NAOK==\"12\" && Q4b.NAOK==\"5\"))\n" +
" ,2,if(((Q4a.NAOK==\"4\" && Q4b.NAOK==\"5\") || (Q4a.NAOK==\"5\" && Q4b.NAOK==\"4\")|| (Q4a.NAOK==\"6\" && Q4b.NAOK==\"4\")|| \n" +
" (Q4a.NAOK==\"7\" && Q4b.NAOK==\"1\")|| (Q4a.NAOK==\"7\" && Q4b.NAOK==\"2\")|| (Q4a.NAOK==\"8\" && Q4b.NAOK==\"5\")|| \n" +
" (Q4a.NAOK==\"9\" && Q4b.NAOK==\"6\")|| (Q4a.NAOK==\"9\" && Q4b.NAOK==\"7\")|| (Q4a.NAOK==\"10\" && Q4b.NAOK==\"6\")|| \n" +
" (Q4a.NAOK==\"11\" && Q4b.NAOK==\"5\")|| (Q4a.NAOK==\"12\" && Q4b.NAOK==\"1\")|| (Q4a.NAOK==\"12\" && Q4b.NAOK==\"2\")|| \n" +
" (Q4a.NAOK==\"12\" && Q4b.NAOK==\"3\")|| (Q4a.NAOK==\"12\" && Q4b.NAOK==\"4\"))\n" +
" ,3,if(((Q4a.NAOK==\"2\" && Q4b.NAOK==\"6\") || (Q4a.NAOK==\"2\" && Q4b.NAOK==\"7\")|| (Q4a.NAOK==\"3\" && Q4b.NAOK==\"6\")|| \n" +
" (Q4a.NAOK==\"3\" && Q4b.NAOK==\"7\")|| (Q4a.NAOK==\"4\" && Q4b.NAOK==\"4\")|| (Q4a.NAOK==\"5\" && Q4b.NAOK==\"3\")|| \n" +
" (Q4a.NAOK==\"6\" && Q4b.NAOK==\"2\")|| (Q4a.NAOK==\"6\" && Q4b.NAOK==\"3\")|| (Q4a.NAOK==\"8\" && Q4b.NAOK==\"4\")|| \n" +
" (Q4a.NAOK==\"9\" && Q4b.NAOK==\"5\")|| (Q4a.NAOK==\"10\" && Q4b.NAOK==\"5\")|| (Q4a.NAOK==\"11\" && Q4b.NAOK==\"4\"))\n" +
" ,4,if(((Q4a.NAOK==\"2\" && Q4b.NAOK==\"4\") || (Q4a.NAOK==\"2\" && Q4b.NAOK==\"5\")|| (Q4a.NAOK==\"3\" && Q4b.NAOK==\"4\")|| (Q4a.NAOK==\"3\" && Q4b.NAOK==\"5\")|| \n" +
" (Q4a.NAOK==\"4\" && Q4b.NAOK==\"3\")|| (Q4a.NAOK==\"5\" && Q4b.NAOK==\"2\")|| (Q4a.NAOK==\"6\" && Q4b.NAOK==\"1\")|| \n" +
" (Q4a.NAOK==\"9\" && Q4b.NAOK==\"4\")|| (Q4a.NAOK==\"10\" && Q4b.NAOK==\"3\")|| (Q4a.NAOK==\"10\" && Q4b.NAOK==\"4\")|| \n" +
" (Q4a.NAOK==\"11\" && Q4b.NAOK==\"1\")|| (Q4a.NAOK==\"11\" && Q4b.NAOK==\"2\")|| (Q4a.NAOK==\"11\" && Q4b.NAOK==\"3\"))\n" +
" ,5,if(((Q4a.NAOK==\"1\" && Q4b.NAOK==\"4\") || (Q4a.NAOK==\"1\" && Q4b.NAOK==\"5\")|| (Q4a.NAOK==\"1\" && Q4b.NAOK==\"6\")|| \n" +
" (Q4a.NAOK==\"1\" && Q4b.NAOK==\"7\")|| (Q4a.NAOK==\"2\" && Q4b.NAOK==\"3\")|| (Q4a.NAOK==\"3\" && Q4b.NAOK==\"2\")|| \n" +
" (Q4a.NAOK==\"3\" && Q4b.NAOK==\"3\")|| (Q4a.NAOK==\"4\" && Q4b.NAOK==\"1\")|| (Q4a.NAOK==\"4\" && Q4b.NAOK==\"2\")|| \n" +
" (Q4a.NAOK==\"5\" && Q4b.NAOK==\"1\")|| (Q4a.NAOK==\"8\" && Q4b.NAOK==\"1\")|| (Q4a.NAOK==\"8\" && Q4b.NAOK==\"2\")|| \n" +
" (Q4a.NAOK==\"8\" && Q4b.NAOK==\"3\")|| (Q4a.NAOK==\"9\" && Q4b.NAOK==\"1\")|| (Q4a.NAOK==\"9\" && Q4b.NAOK==\"2\")|| \n" +
" (Q4a.NAOK==\"9\" && Q4b.NAOK==\"3\")|| (Q4a.NAOK==\"10\" && Q4b.NAOK==\"1\")|| (Q4a.NAOK==\"10\" && Q4b.NAOK==\"2\"))\n" +
" ,6,if(((Q4a.NAOK==\"1\" && Q4b.NAOK==\"3\") || (Q4a.NAOK==\"2\" && Q4b.NAOK==\"2\"))\n" +
" ,7,if(((Q4a.NAOK==\"1\" && Q4b.NAOK==\"1\") || (Q4a.NAOK==\"1\" && Q4b.NAOK==\"2\") || (Q4a.NAOK==\"2\" && Q4b.NAOK==\"1\") || \n" +
" (Q4a.NAOK==\"3\" && Q4b.NAOK==\"1\"))\n" +
" ,8,\"\"))))))))}";
String answer = getAnswer(answer1, answer2, s);
Log.d("xxx", "answer:" + answer);
}
private String getAnswer(int answer1, int answer2, String s) {
//remove spaces 2 or more length and replace to 1
s = s.replaceAll("\n", "");
s = s.replaceAll("\\s+", " ");
ArrayList<String> states = new ArrayList<String>();
Pattern p = Pattern.compile("if\\({3}.*?\\,\\d+");
Matcher m = p.matcher(s);
while (m.find()) {
states.add(m.group());
}
String stateFormat = "Q4a.NAOK==\"%d\" && Q4b.NAOK==\"%d\"";
String stateFormatted = String.format(stateFormat, answer1, answer2);
for (int i = 0; i < states.size(); i++) {
if (states.get(i).contains(stateFormatted)) {
String[] items = states.get(i).split(",");
if (items.length > 1) {
return items[1].trim();
}
}
}
return null;//or null or integer -1
}