如何在异步存储中获取有价物品?

时间:2020-05-02 04:31:32

标签: javascript react-native react-native-android react-navigation react-native-ios

在我的日志中,值是 {“ namecity”:“ India”} ,但是我只希望印度具有我的值,如何获得它

AsyncStorage.getItem('cityname').then((cityname) => {
    if (cityname) {
        this.setState({ city: cityname });
        console.log(this.state.city);
    }
});

1 个答案:

答案 0 :(得分:2)

我认为您的对象是JSON编码的。使用JSON解析,您就可以访问键值。

AsyncStorage.getItem('cityname').then((cityname) => {
        if(cityname){
            const decodedCity = JSON.parse(cityname);
            this.setState({city: decodedCity['namecity']});
            console.log(this.state.city);
        }
});