如何将响应json数据的值保存到AsyncStorage?

时间:2019-08-17 05:46:05

标签: json react-native

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”}]

1 个答案:

答案 0 :(得分:0)

AsyncStorage与String一起使用。 您需要保存:

JSON.stringify(response.data)

在其中。

类似这样的东西:

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