【问题标题】:AsyncStorage returns array to string react-nativeAsyncStorage 将数组返回到字符串 react-native
【发布时间】:2021-08-27 23:02:07
【问题描述】:

我有一个数组作为 Json.stringfy 存储在 AsyncStorage 中。当我尝试获取它作为字符串返回的密钥时。我想要的是这样的[0:'apple', 1:'ball', 2:'car']。以下是我尝试过的方法

  useEffect(() => {
     AsyncStorage.getItem('@moduke').then(module => {
        const a = JSON.parse(module);
        console.log(a.length);
     });
  }, []);

【问题讨论】:

    标签: android arrays objective-c react-native asyncstorage


    【解决方案1】:

    字符串被“”包围。并使用“。当尝试通过JSON.parse()解析上述字符串时,仍然返回字符串:{"0:'apple', 1:'ball', 2:'car'}\"被替换为”。但是再次使用 JSON.parse(),它将返回我解决的对象,如下方法

         useEffect(() => {
             AsyncStorage.getItem('@moduke').then(module => {
                const a = JSON.parse(module);
                const b = JSON.parse(a);
                console.log(b);
                console.log(b.length);
             });
          }, []);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-06
      • 2018-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多