我对javascript还是很陌生,所以请忍受。
我正在解析JSON,并希望将对象存储到本地存储中。我有storage.set和console.log。日志记录了所有对象,但是当我检查本地存储时,仅存储了最后一个对象。
JSON:
[
{"id": 169, "year": "1997", "titles": [ "Sedm let v Tibetu", "Seven Years in Tibet", "Sedem rokov v Tibete" ]},
{"id": 251, "year": "1949", "titles": [ "Šaty ze sedmi rákosů", "Sette canne, un vestito", "Seven Reeds, One Suit" ]},
{"id": 404, "year": "1974", "titles": [ "Sedm zlatých upírů", "The Legend of the 7 Golden Vampires", "7 Golden Vampires", "Seven Golden Vampires: The Last Warning", "Qi jin shi", "七金屍", "The 7 Brothers Meet Dracula", "The Seven Brothers Meet Dracula" ]},
{"id": 723, "year": "1957", "titles": [ "Sedmá pečeť", "Det sjunde inseglet", "Riddaren och döden", "Siedma pečať", "The Seventh Seal" ]},
{"id": 1417, "year": "1967", "titles": [ "Sedm havranů" ]},
{"id": 1418, "year": "1974", "titles": [ "Sedmého dne večer" ]},
{"id": 1542, "year": "1966", "titles": [ "Sedmikrásky", "Sedmokrásky", "Daisies" ]},
{"id": 2013, "year": "1940", "titles": [ "Pán sedmi moří", "The Sea Hawk" ]},
{"id": 2292, "year": "1999", "titles": [ "Zelená míle", "The Green Mile", "Zelená míľa" ]}
]
代码如下:
var JSONURL = chrome.runtime.getURL("final.json");
requestJSON(JSONURL);
function requestJSON(url) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.onload = function() {
if (xhr.status === 200) {
var json = xhr.responseText;
jsonParsed = JSON.parse(json);
jsonParsed.forEach(function(object) {
storage.set(object);
console.log(object);
// if (object.id === 2294) {
// console.log(object.year);
// }
});
}
};
xhr.send();
return true;
}
然后我使用下面的代码使用后台页面检查本地存储时,它仅返回JSON中的最后一个对象:
chrome.storage.local.get(function(result){console.log(result)})
Returns:
{id: 2292, titles: Array(3), year: "1999"}
id: 2292
titles: (3) ["Zelená míle", "The Green Mile", "Zelená míľa"]
year: "1999"
__proto__: Object
我在做什么错?似乎每次将对象存储集时,它都会重写最后一个对象。怎么会来?
JSON格式不正确吗?
编辑: 我更新了JSON,使其看起来像这样:
[
{"169": {"year": "1997", "titles": [ "Sedm let v Tibetu", "Seven Years in Tibet", "Sedem rokov v Tibete" ]}},
{"251": {"year": "1949", "titles": [ "Šaty ze sedmi rákosů", "Sette canne, un vestito", "Seven Reeds, One Suit" ]}}
]
现在,它可以正确保存每个ID。但是我遇到了另一个问题,我可以通过ID进行storage.get,但是无法获取例如year属性:
storage.get("169", function(result){
console.log(result.year);
});
当我只做console.log(result);
时,它会起作用。
EDIT2: 还尝试过使用这样的JSON:
[
{"test": [{"id": 169, "year": "1997", "titles": [ "Sedm let v Tibetu", "Seven Years in Tibet", "Sedem rokov v Tibete" ]}, {"id": 251, "year": "1949", "titles": [ "Šaty ze sedmi rákosů", "Sette canne, un vestito", "Seven Reeds, One Suit" ]}]}
]
当我console.log(result);
正确返回时,如下所示,但是我仍然无法访问其中的任何内容。可能是因为我不知道...
{test: Array(2)}
test: Array(2)
0: {id: 169, titles: Array(3), year: "1997"}
1: {id: 251, titles: Array(3), year: "1949"}
length: 2
__proto__: Array(0)
__proto__: Object
尝试过foreach,尝试遍历对象数组,无济于事。
解决了,多亏了wOxxOm先生!
代码如下:
var storage = chrome.storage.local;
var JSONURL = chrome.runtime.getURL("final.json");
requestJSON(JSONURL);
var titleID = "169"
//storage.get(titleID, d=> console.log(d['169'].year));
storage.get(titleID, function(result){
var titles = result[titleID].titles;
titles.forEach(function(item, i) {
console.log(item);
})
});
function requestJSON(url) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
//xhr.withCredentials = true;
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.onload = function() {
if (xhr.status === 200) {
var json = xhr.responseText;
jsonParsed = JSON.parse(json);
jsonParsed.forEach(function(object) {
storage.set(object);
});
}
};
xhr.send();
return true;
}