首页 > 解决方案 > 如何将响应 json 数据的值保存到 AsyncStorage?

问题描述

constructor(props) { 
  super(props) 
  this.state = { 
    UserEmail: '',
    UserPassword: '' 
  } 
} 

UserLoginFunction = () => { 
  const { UserEmail }  = this.state;
  const { UserPassword }  = this.state; 
  axios.post(`Login`, { 
    username: this.state.UserEmail, 
    password: this.state.UserPassword
  })
  .then(response => {
    console.log(response.data);
  })
  .catch((error) => {
    console.error(error);    
  }); 
}                                        

回复:

[{"return_type":"true","role_id":"1","users_id":"1","manpower_id":"1","message":"Success"}]

标签: jsonreact-native

解决方案


AsyncStorage 与字符串一起使用。您需要保存:

JSON.stringify(response.data)

在里面。

像这样的东西:

const data = JSON.stringify(response.data);
await AsyncStorage.setItem('key or any name you want', data);

推荐阅读