Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法显示来自webservices调用的响应(代号为1)_Java_Codenameone - Fatal编程技术网

Java 无法显示来自webservices调用的响应(代号为1)

Java 无法显示来自webservices调用的响应(代号为1),java,codenameone,Java,Codenameone,我从我的Web服务中得到这个JSON响应 {root:[{name:abc,姓氏:xyz}]}我试图使用label仅显示响应中的Key:Value 我正在使用addcomponent方法 hi.addComponent(new Label(""+ response.get("/result[0]/ocassion"))); 但它显示的是空值。我关注了codenameone网站上提供的Web服务视频 我应该尝试什么来获得在UI上显示的所需响应。 下面是来自codename one的MyAppli

我从我的Web服务中得到这个JSON响应
{root:[{name:abc,姓氏:xyz}]}
我试图使用label仅显示响应中的Key:Value

我正在使用addcomponent方法

hi.addComponent(new Label(""+ response.get("/result[0]/ocassion")));
但它显示的是空值。我关注了codenameone网站上提供的Web服务视频

我应该尝试什么来获得在UI上显示的所需响应。 下面是来自codename one的
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
}

你好谢谢你的代码。它按我的要求工作。但现在我正在对标签做一些更改,希望使用多个按钮,这样当按下按钮时,它将显示名称和姓氏。你能建议我怎么做吗。