0

我目前正在从 Java ASK-SDK v1 迁移到Java ASK SDK v2

我正在尝试使用ResponseBuilder我构建响应的类返回一个 webhook 调用,并且数据是正确的,但是当我尝试使用 JSON 文本填充 HTTP 正文时,该ResponseBuilder.toString()值不仅仅使用字符串,我得到以下信息:

Optional[class Response {
    outputSpeech: class SsmlOutputSpeech {
        class OutputSpeech {
            type: SSML
            playBehavior: null
        }
        ssml: <speak>Some of the things you can say are What would you like to do?</speak>
    }
    card: null
    reprompt: class Reprompt {
        outputSpeech: class SsmlOutputSpeech {
            class OutputSpeech {
                type: SSML
                playBehavior: null
            }
            ssml: <speak>You can say ..., is that what you want?</speak>
        }
    }
    directives: []
    shouldEndSession: false
    canFulfillIntent: null
}]

是否有另一种方法来获取响应正文的字符串?BaseSkillResponse一个getResponse()调用,但是,我无法弄清楚如何使用该类来生成字符串响应输出。

4

2 回答 2

0

我能够在课堂上获得以下字符串:

 private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

 myFunction(){
     try{
         return toJsonString(responseBuilder.build().get());
     } catch(IOException e) {
         e.printStackTrace();
     }
 }

 public String toJsonString(Response response)throws IOException {
     return OBJECT_MAPPER.writeValueAsString(response);
 }
于 2019-12-06T20:25:39.553 回答
0

通过执行以下操作解决此问题:

        public String toJsonString(Response response)throws IOException 
        {
          JacksonSerializer jacksonSerializer = new JacksonSerializer();
          constructedResponse = jacksonSerializer.serialize(response);

          JSONObject jsonObject = new JSONObject();

          jsonObject.put("response",constructedResponse);
        }
于 2019-12-12T15:39:06.927 回答