在JavaScript中使用JSON将数组存储在localStorage中 [英] Storing array in localStorage with JSON in javascript

查看:301
本文介绍了在JavaScript中使用JSON将数组存储在localStorage中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经参考了这个问题并为我工作:所以q1

I have refereed this question and worked for me: so q1

现在的问题是,我已经使用JSON.stringify& JSON.parse将数组存储在localStorage中.但是,当我再次运行代码并尝试在localStorage上使用JSON.parse时,则不会重新获取任何内容.我不知道怎么了.

Now Issue is that I have used JSON.stringify & JSON.parse to store array in localStorage. But when I run the code again and try to use JSON.parse on localStorage nothing get retrived. I dont know whats going wrong.

这是代码:

    function BankProDisplay() {
        var myString = JSON.parse(localStorage['bankpro']);
        var mySplitResult = myString.split(",");
        for (i = 0; i < mySplitResult.length; i++) {
            document.getElementById('bankpro').innerHTML = document.getElementById('bankpro').innerHTML + mySplitResult[i] + "<br/>";
        }
    }

推荐答案

var myString = JSON.parse(localStorage['bankpro']);

JSON.parse返回一个没有split方法的javascript对象,而不是字符串.

JSON.parse returns a javascript object, not a string, which would not have a split method.

这篇关于在JavaScript中使用JSON将数组存储在localStorage中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆