gpt4 book ai didi

javascript - 放(存)json数据

转载 作者:行者123 更新时间:2023-11-29 21:33:01 25 4
gpt4 key购买 nike

我有 json 数据:

{ "products": [

{
"productId" : 0,
"productImg" : "../img/product-photo1.png",
"productName" : "New Balance",
"productNameNumber" : "(1st)",
"productSeria" : "Ref. 2514/302",
"productColor" : "Grey",
"productSize" : "38",
"productAmount" : "€32.20"
},
{
"productId" : 1,
"productImg" : "../img/product-photo2.png",
"productName" : "New Balance",
"productNameNumber" : "(2st)",
"productSeria" : "Ref. 2611/502",
"productColor" : "White",
"productSize" : "39",
"productAmount" : "€20.10"
}
]}

我需要将这些数据放到本地存储中。我已经找到了足够的信息来说明如何将数据从 js 对象放入本地存储,但是如何将数据从 json 文件中放入我还没有找到。请帮我找出来。

最佳答案

使用 JSON.stringify() 将您的对象转换为字符串。然后保存到localStorage

例如:

var obj = { 
"products": [
{
"productId" : 0,
"productImg" : "../img/product-photo1.png",
"productName" : "New Balance",
"productNameNumber" : "(1st)",
"productSeria" : "Ref. 2514/302",
"productColor" : "Grey",
"productSize" : "38",
"productAmount" : "€32.20"
},
{
"productId" : 1,
"productImg" : "../img/product-photo2.png",
"productName" : "New Balance",
"productNameNumber" : "(2st)",
"productSeria" : "Ref. 2611/502",
"productColor" : "White",
"productSize" : "39",
"productAmount" : "€20.10"
}
]
};

localStorage.setItem('someKey', JSON.stringify(obj));

console.log(JSON.parse(localStorage.getItem('someKey')));

关于javascript - 放(存)json数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35638498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com