I have the following url:
http://domain.com/details.aspx?number=2012-001
I want to match the 'number' param in the URL string and display only the JSON data associated to it so in this case I only want to render the first object within the JSON array.
Here's the JSON structure:
{
"notices": [
{
"number": "2012-001",
"link": "www.google.com",
"title": "sample title",
"awardClaimDueDate": "3/1/2015",
"awardClaimForms": "abc.pdf",
"datePosted": "1/31/2012"
},
{
"number": "2012-002",
"link": "www.yahoo.com",
"title": "sample title",
"awardClaimDueDate": "4/3/2015",
"awardClaimForms": "file.doc",
"datePosted": "2/3/2012"
}
]
}
I've attempted to write the JS but I'm having difficulty displaying only the values associated to the number. Still a noob so your help would be appreciated!
function jsonParser(json){
$('#load').fadeOut();
$.getJSON('notices.json',function(data){
// Parse ID param from url
var noticeParamID = getParameterByName('number');
$.each(data.notices, function(k,v){
var noticeNumber = v.number,
noticeTitle = v.title,
claimDueDate = v.awardClaimDueDate,
claimForms = v.awardClaimForms,
date = v.datePosted;
if(noticeParamID == noticeNumber){
// how can I display content that matches the url param value (noticeURLNumber)?
}
});
});
}
// get URL parameter by name
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
$("#someDIV").html(content);to display it. What's the problem?