使用java在ComboBox中显示JSON数据

时间:2015-10-21 11:33:24

标签: java json

我使用下面的代码检索json数据并将其放在表格中。那很有效。但我想现在在组合框中显示数据。现在,它以这种格式显示下拉列表中的项目:ljava.lang.string @ 5c647e05 有人可以告诉我我做错了什么吗?谢谢。

                        Hashtable response = parser.parse(reader);

                        java.util.List allResult = (java.util.List) response.get("AllResult");
                        System.out.println(allResult);
                        try {

                            String[][] data = new String[allResult.size()][4];
                            for (int i = 0; i < allResult.size(); i++) {

                                Object obj = allResult.get(i);
                                String result = (String) ((Hashtable) obj).get("Status");

                                String investorName = (String) ((Hashtable) obj).get("investorName");
                                if (result.equalsIgnoreCase("ok")) {
                                    for (int j = 0; j < 4; j++) {

                                        if (j == 0) {
                                            data[i][j] = investorName;                          }
                                                                            }

                                }
                            }

                            ComboBox investorNames = new ComboBox(data);
                            details.addComponent(investorNames);
                            System.out.println("Data here: " + data);

                            String[] columnNames = { "Seller's Name", "Stock Name", "Unit", "Price" };
                            TableModel model = new DefaultTableModel(columnNames, data, false);
                            Table table = new Table(model);
                            table.setScrollable(true);

                            details.addComponent(table);
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }

                    }

2 个答案:

答案 0 :(得分:0)

尝试仅粘贴invector名称的 Combobox 向量,而不是整个数组数组。由于 Combobox 只是在矩阵的每个数组(行)上调用 toString(),这就是为什么你得到 Ljava.lang.String@5c647e05 而不是投资者姓名。像这样:

String[] comboData = new String[allResult.size()];
for (int i = 0; i < allResult.size(); i++){

    ...

    String investorName = (String) ((Hashtable) obj).get("investorName");

    comboData[i] = investorName

    ...

}

ComboBox investorNames = new ComboBox(comboData);
details.addComponent(investorNames);

....

答案 1 :(得分:0)

ComboBox需要一个字符串数组,但是你要传递一个字符串数组数组。

所以你看到的那些ljava.lang.string @ 5c647e05就是&#39; toString&#39;表示字符串数组。

你需要以某种方式(取决于你需要做什么)压扁你的数据&#39;这样它就变成了一个字符串数组。