0
我遇到了一個問題,我需要從JSON文件中抓取並存儲所有三個「house」和「exposure」,這取決於URL是否具有ID-1或ID-2等等。我將它們放在.each循環中,正確地迭代.JSON Listening以獲取在URL中傳遞的正確參數。例如, 。 http://localhost/?houseblock=ID-1現在只會加載ID-1內部的JSON。但我需要取回並儲存以備後用三個「房屋」和「暴露」號碼。從.getJson多個.each循環存儲多個對象
我有點不熟悉如何正確地做到這一點,我想告訴。每一個循環後抓住「房子」和「曝光」,然後在第二個循環再次做次要的「房子「和」暴露「。但似乎並非如此。
JSON jsonFile.json
{
"ID-1":{
"1-1":{
"house":6,
"exposure":1
},
"1-2":{
"house":1,
"exposure":3
},
"1-3":{
"house":3,
"exposure":2
}
},
"ID-2":{
"2-1":{
"house":3,
"exposure":2
},
"2-2":{
"house":6,
"exposure":5
},
"2-3":{
"house":1,
"exposure":1
}
}
}
JQUERY
$(document).ready(function()
{
// -- Listen to URL Changes --//
var urlListen;
(window.onpopstate = function()
{
var match,
pl = /\+/g, // Regex for replacing addition symbol with a space
search = /([^&=]+)=?([^&]*)/g,
decode = function(s)
{
return decodeURIComponent(s.replace(pl, "-"));
},
query = window.location.search.substring(1);
urlListen = {};
while (match = search.exec(query)) urlListen[decode(match[1])] = decode(match[2]);
})();
var retrieveAll = $.getJSON('jsonFile.json', function(data) {}).done(function(data)
{
$(data[urlListen["houseblock"]]).each(function(i, val)
{
$.each(val, function(j, b)
{
$.each(b, function(k, v)
{
console.log(k + " : " + v);
if (k == "house")
{
houseObj = v;
houseObj2 = v;
houseObj3 = v;
}
});
});
});
}).fail(function(data)
{
console.log("error");
}).always(function(data)
{
console.log("complete");
});
retrieveAll.complete(function(data)
{
init();
});
});
function init()
{
console.log(houseObj);
console.log(houseObj2);
console.log(houseObj3);
}