JSON Parse Complex Object from User Local Storage with Node Persists [已关闭]

oxf4rvwz  于 2023-07-01  发布在  其他
关注(0)|答案(1)|浏览(128)

**已关闭。**此问题不符合Stack Overflow guidelines。目前不接受答复。

这个问题似乎不是关于在help center定义的范围内编程。
2天前关闭。
Improve this question
我使用vanilla JavaScript和node-persist将数据存储在用户的本地驱动器中。尝试从存储中提取数据会导致以下文本:

{
    "week":{
        "startDay":"2023-06-28",
        "days":[
            {"startDate":"2023-06-28","dayId":"thursday"},
            {"startDate":"2023-06-29","dayId":"friday"},
            {"startDate":"2023-06-30","dayId":"saturday"},
            {"startDate":"2023-07-01","dayId":"sunday"},
            {"startDate":"2023-08-01","dayId":"monday"},
            {"startDate":"2023-09-02","dayId":"tuesday"},
            {"startDate":"2023-10-04","dayId":"wednesday"}
            ]
        }
    }

但是使用JSON.parse()创建一个对象却不能生成正确的week对象。有没有更好的方法来a)将对象放入存储中b)从存储中解析对象?

yftpprvb

yftpprvb1#

我会像这样存储对象:

window.localStorage.setItem("week", JSON.stringify(week));

就像这样

let week = JSON.parse(window.localStorage.getItem("week"));

希望这就是你要找的:)

相关问题