java - 无法显示代号为 1 的 Web 服务调用的响应

标签 java codenameone

我从我的网络服务中得到这个 JSON 响应 {root:[{name:abc, surname:xyz}]} 并且我尝试仅显示来自使用标签的响应的 Key:Value。

我正在使用添加组件的方法

hi.addComponent(new Label(""+ response.get("/result[0]/ocassion")));

但它向我显示空值。我关注了 codenameone 网站上有关 Web 服务的视频。

我应该尝试什么才能在 UI 上显示所需的响应。 这是代号一的 MyApplication.java 代码:

InputStreamReader reader = new InputStreamReader(input);
JSONParser parser = new JSONParser();
response = parser.parse(reader);
System.out.println(""+ response);
hi.addComponent(new Label(""+ response.get("/result[0]/ocassion")));
hi.getComponentForm().revalidate();

最佳答案

parse.parse 更改为 parse.parseJSON。您必须知道检索值的键,您可以按如下方式循环响应:

InputStreamReader reader = new InputStreamReader(input);
JSONParser parser = new JSONParser();
response = parser.parseJSON(reader);

List listResponses = (List) response.get("root");
if (listResponse != null) {
    for (Object listResponse : listResponses) {
        Map tempHash = (Map) listResponse;
        hi.addComponent(new Label("Name: " + tempHash.get("name").toString())); 
        hi.addComponent(new Label("Surname: " + tempHash.get("surname").toString())); 
        System.out.println(tempHash.get("name").toString()); //Print abc
        System.out.println(tempHash.get("surname").toString()); //Print xyz
    }
    hi.getComponentForm().revalidate();
} else {
    System.out.println("null value returned"); //Make sure you reference root and be sure it returns proper json
}

关于java - 无法显示代号为 1 的 Web 服务调用的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34410128/

相关文章:

java - Google map Activity 显示空白屏幕

java - 如何使 libGDX 桌面应用程序可调整大小?

java - 代号一,更改代码内多按钮文本的颜色,而不是 GUI

sqlite - codenameone 在 sqlite 中将 boolean 值存储为 true/false,而它们应该是 1/0

java - 如何在表达式语言中格式化数字?

java - 获取当前正在运行的 Activity 的上下文

java - 在数组中查找多对(两对和/或葫芦)

android - 代号一。 IOS 拦截 URL 问题。

ios - Codenameone中如何从Storage复制到FileSystemStorage并显示在BrowserComponent中

ios - 代号一-从iOS设备获取IMEI