Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.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
使用亚马逊&x27;在Java Alexa技能工具包中,我可以将自定义Java对象放入会话属性中吗?_Java_Session_Attributes_Alexa - Fatal编程技术网

使用亚马逊&x27;在Java Alexa技能工具包中,我可以将自定义Java对象放入会话属性中吗?

使用亚马逊&x27;在Java Alexa技能工具包中,我可以将自定义Java对象放入会话属性中吗?,java,session,attributes,alexa,Java,Session,Attributes,Alexa,我正在使用Alexa Skill Kit for Java构建自定义技能。我试图在com.amazon.speechlet.session中使用自定义会话属性co.prosody.util.PaInputData session.setAttribute(“inputData”,paInputData)没有问题,但是当我检索属性(paInputData)session.getAttribute(“inputData”)时,我得到一个类强制转换异常: “errorMessage”:“java.ut

我正在使用Alexa Skill Kit for Java构建自定义技能。我试图在
com.amazon.speechlet.session
中使用自定义会话属性
co.prosody.util.PaInputData

session.setAttribute(“inputData”,paInputData)
没有问题,但是当我检索属性
(paInputData)session.getAttribute(“inputData”)
时,我得到一个类强制转换异常:

“errorMessage”:“java.util.LinkedHashMap无法转换为co.prosody.util.PaInputData”


可以取回我的对象吗?

属于会话的非字符串属性对象将转换为链接的哈希映射,将每个实例变量的名称映射到其值的名称。对于不太复杂的对象,您可以通过访问与属于会话属性的该对象关联的链接哈希映射的每个键来获取这些值,然后以这种方式重新构造该对象。(请注意,您将需要与实例变量关联的getter)但是,您无法将会话属性中存储的任何对象直接转换为您想要/期望的对象

比如说,我有一个Dog对象,有两个字段:

public class Dog{
    private String name = "Spot";
    private int age = 20;
    public Dog(){

    }
    public Dog(String nIn, int aIn){
        name = nIn;
        age = aIn;
    }
    public int getAge(){
        return age;
    }
    public String getName(){
        return name;
    }


}
如果使用“my_DOG”键将其存储在会话的属性中,我可以通过以下方式重建对象:

LinkedHashMap<String, Object> myDog = (LinkedHashMap<String, Object>)session.getAttribute("MY_DOG");
String fetchedName = (String)myDog.get("name"); //this will return "Spot"
int fetchedAge = (Integer)myDog.get("age"); //this will return 20
Dog copyDog = new Dog(fetchedName, age);//effectively copies the Dog object
LinkedHashMap myDog=(LinkedHashMap)session.getAttribute(“MY_DOG”);
String fetchedName=(String)myDog.get(“name”)//这将返回“点”
int fetchedAge=(整数)myDog.get(“年龄”)//这将返回20
狗复制狗=新狗(fetchedName,年龄)//有效地复制狗对象

属于非字符串会话的属性对象将转换为链接的哈希映射,将每个实例变量的名称映射到其值的名称。对于不太复杂的对象,您可以通过访问与属于会话属性的该对象关联的链接哈希映射的每个键来获取这些值,然后以这种方式重新构造该对象。(请注意,您将需要与实例变量关联的getter)但是,您无法将会话属性中存储的任何对象直接转换为您想要/期望的对象

比如说,我有一个Dog对象,有两个字段:

public class Dog{
    private String name = "Spot";
    private int age = 20;
    public Dog(){

    }
    public Dog(String nIn, int aIn){
        name = nIn;
        age = aIn;
    }
    public int getAge(){
        return age;
    }
    public String getName(){
        return name;
    }


}
如果使用“my_DOG”键将其存储在会话的属性中,我可以通过以下方式重建对象:

LinkedHashMap<String, Object> myDog = (LinkedHashMap<String, Object>)session.getAttribute("MY_DOG");
String fetchedName = (String)myDog.get("name"); //this will return "Spot"
int fetchedAge = (Integer)myDog.get("age"); //this will return 20
Dog copyDog = new Dog(fetchedName, age);//effectively copies the Dog object
LinkedHashMap myDog=(LinkedHashMap)session.getAttribute(“MY_DOG”);
String fetchedName=(String)myDog.get(“name”)//这将返回“点”
int fetchedAge=(整数)myDog.get(“年龄”)//这将返回20
狗复制狗=新狗(fetchedName,年龄)//有效地复制狗对象