나는 없습니다. 확인란의 선택 (20), 내가 뭘했는지 사용자가 어떤 확인란을 선택하면, 그 이름은 배열에 저장됩니다 (예 : 코드에서 abc 배열). 해당 json을 저장하는 문자열 변수의 이름은 확인란의 이름과 같습니다. 예를 들어 체크 박스 "a"를 클릭하면 문자열 값 "a"가 배열에 저장되며 관련 json 값을 저장하는 "a"라는 문자열 변수가 있습니다. 필요한 것은 배열에 저장된 문자열 값을 InputStream = new ByteArrayInputStream (abc.get (i) .getBytes())로 전달하면 json의 inputStream을 구문 분석하는 데 사용해야합니다. 그러나 문자열 값 "a"가 문자열 변수 a와 같지 않기 때문에 NullPointerException을 제공합니다. 이 문제를 어떻게 해결할 수 있습니까? 나는 여기서 아이디어가 떨어졌다. 여기서하고 싶은 것을 성취 할 수있는 다른 방법이 있습니까?배열의 문자열 값을 문자열 변수로 사용하여 json-codenameone을 구문 분석합니다.
코드 : 선택된 체크 박스의 문자열 값은 JSON 파서 배열
String a = "[{\n"
+ "\t\t\t\"title\": \"title1\",\n"
+ "\t\t\t\"describ\": \"describ1\"\n"
+ "}]";
String b = "[{\n"
+ "\"title\": \"title2\",\n"
+ "\"describ\": \"describ2\"\n"
+ "}]";
String c = "[{\n"
+ "\t\t\t\"title\": \"title3\",\n"
+ "\t\t\t\"describ\": \"describ3\"\n"
+ "}]";
//and all jsons required are there
ArrayList<String> abc;
@Override
protected void beforeTestForApp(Form f) {
f.setTitle("abc");
abc = new ArrayList<>();
//I have stored "a" & "b" in the abc array here for simplicity, but it is dynamic,
//ie. if the user select checkbox c, c will be stored in abc array and so on
abc.add("a");
abc.add("b");
Button bb = new Button("go");
bb.addActionListener((e) -> {
showForm("TestForAppResult", null);
});
f.add(bb);
}
형태로 저장하고 값 표시 :
@Override
protected void beforeTestForAppResult(Form f) {
f.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
InputStream is;
for (int i = 0; i < abc.size(); i++) {
Label heading = new Label(abc.get(i));
f.add(heading);
//this gives error since abc.get(i) gives string value, not string variable
is = new ByteArrayInputStream(abc.get(i).getBytes());
showDetails(is, f);
}
//if i do this instead of for loop jst above, i can get the result but since what value'll be stored in an array is not known,it is not possible
//is = new ByteArrayInputStream(a.getBytes());
//showDetails(is, f);
//is = new ByteArrayInputStream(b.getBytes());
//showDetails(is, f);
}
private void showDetails(InputStream is, Form f) {
JSONParser p = new JSONParser();
Hashtable<String, Object> test;
try {
test = p.parse(new InputStreamReader(is));
Vector aVector = (Vector) test.get("root");
for (int j = 0; j < aVector.size(); j++) {
Hashtable hm = (Hashtable) aVector.get(j);
String title = (String) hm.get("title");
String describ = (String) hm.get("describ");
Label z = new Label(title);
Label zz = new Label(describ);
f.add(z);
f.add(zz);
}
} catch (IOException ex) {
}
}
의 짧은 조각입니다 ... 대신 구문 분석의 parseJSON를 사용합니다. json이 저장된 문자열 예 : 문자열 a = "[{json}]", 문자열 b = "[{other json}] 등은 객체의 값과 동일한 이름을 가짐 사용자가 값"a " , 나는 json 문자열 변수를 구문 분석 할 & 등등. 또한 하나의 여러 개체를 선택할 수 있고 json 동시에 구문 분석해야합니다. – beck
두 질문 및이 주석을 읽었습니다 및 나는 당신이 무엇을 요구하는 모르겠다. .. –