私はnoです。チェックボックス(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) {
}
}
の短い抜粋です。 jsonが格納されている文字列String a = "[{json}]"、String b = "[{other json}]などはオブジェクトの値と同じ名前を持ちます。さらに、jsonの文字列変数を解析する必要があります。さらに、複数のオブジェクトを選択でき、同時にjsonを解析する必要があります。 – beck
質問とこのコメントの両方を読みましたが、何を求めているのかわかりません。 .. –