我想在本地为每个以json格式返回的子对象存储数据,如下所示 - `key - name sectionType +节类型数,值 - 此对象
我现有的JS代码:
function syncAllSections_4(){
user_sections= loggeduser_array.id;
console.log(user_sections);
var dict_4 = { userId: user_sections };
$.ajax({
url: 'http://funiks.com/adminv7/offline-api/listSections.php',
type: 'POST',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
data: JSON.stringify(dict_4),
})
.done(function(data, textStatus, jqXHR) {
if(data.status=="SUCCESS") {
localStorage.setItem("sections",JSON.stringify(data.sections));
//console.log(JSON.parse(localStorage.getItem("sections")));
var sections_array = [];
sections_array = JSON.parse(localStorage.getItem("sections"));
}
}).fail(function(jqXHR, textStatus, errorThrown) {
alert("error");
});
}
Json返回的数据格式是:
https://pastebin.com/4XBR23ua
答案 0 :(得分:1)
似乎"部分"已经是一个数组,所以你不应该为数组长度字符串化。此外,解决方案非常简单(未经测试但应该有效):
function syncAllSections_4(){
user_sections= loggeduser_array.id;
console.log(user_sections);
var dict_4 = { userId: user_sections };
$.ajax({
url: 'http://funiks.com/adminv7/offline-api/listSections.php',
type: 'POST',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
data: JSON.stringify(dict_4),
})
.done(function(data, textStatus, jqXHR) {
if(data.status=="SUCCESS") {
data.sections.forEach(section)=>{
localStorage.setItem("SectionTypes"+section.sectionType,JSON.stringify(section));
})
}
}).fail(function(jqXHR, textStatus, errorThrown) {
alert("error");
});
}