我没有。对于复选框(20),我所做的是如果用户选择任何复选框,其名称存储在数组中(例如,代码中的abc数组)。存储相应json的字符串变量的名称与复选框的名称相同。例如,如果Checkbox" a"单击,字符串值" a"存储在数组中,并且有一个名为&#34的字符串变量; a"存储相关的json值。我需要的是,如果我传递存储在数组中的字符串值作为InputStream is = new ByteArrayInputStream(abc.get(i).getBytes()),它应该用于解析json的inputStream。但它给出了NullPointerException,因为字符串值" a"不等于字符串变量a。我怎么解决这个问题?我在这里没有想法了。有没有其他方法可以实现我想要做的事情?
代码:所选复选框的字符串值存储在数组
中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);
}
json解析器的表单并显示值:
@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) {
}
}
答案 0 :(得分:1)
我没有具体地解决你的问题,但我仍然试着给你一些镜头,以便你可以尝试。
如果我理解正确你有20个对象包含潜在的价值? 那么你有一个JSONArray,只需迭代它并抓住那个JSONObject。
现在只使用parseJSON而不是parse,因为它已被弃用...
这是我的代码的简短片段
Intent getContentIntent = new Intent(Intent.ACTION_GET_CONTENT);
getContentIntent.addCategory(Intent.CATEGORY_OPENABLE);
getContentIntent.setType("text/plain");
String [] mimeTypes={"application/vnd.oasis.opendocument.text","application/msword","application/vnd.openxmlformats-officedocument.wordprocessingml.document","application/vnd.openxmlformats-officedocument.wordprocessingml.template","application/vnd.ms-word.document.macroEnabled.12","application/vnd.ms-word.template.macroEnabled.12","text/plain"};
getContentIntent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
Intent intent = Intent.createChooser(getContentIntent, "Import Document");
activity.startActivityForResult(intent, com.test.app.Constants.REQUESTCODES.FILE_CHOOSER.getValue());