If I am to have this external json file:
{
"http://example.org/about" :
{
"http://purl.org/dc/elements/1.1/title" : [{"value" : "annas homepage" , "type" : "literal"}]
}
}
and this external jquery script that fetches title of the book inside json file:
var a = 'http://example.org/about'
var b = 'http://purl.org/dc/elements/1.1/title'
$(document).ready(function() {
$("#trigger").click(function(event) {
$.getJSON('rdfjson.json', function(json) {
$('#meta').html('Title: ' + json.a.b[0].value);
});
});
});
and this HTML code:
<p>Click on the button to fetch data from json structure:</p>
<div id="meta">
This text will be overwritten.
</div>
<input type="button" id="trigger" value="Load Data" />
Why wouldn’t it work? When I use normal strings inside json document and script, it works OK.
Also, I don’t understand how could I iterate through the whole json file if it is, for example, more complex or elements have long vectors containing data etc etc..
Thanks a lot!
Because you’re looking for an object member named
a, rather than an object member whose name is the value stored in the variable “a”.Instead of using “dot notation“, use “square bracket notation“
For more information, see here: http://www.dev-archive.net/articles/js-dot-notation/
Edit:
If the JSON structure varies like so:
Version One:
Version Two:
e.g. there is always one object, which has one member which has one member
… and you need to target it:
You can of course add conditionals within the
forloops if you know a little bit more information about the path you need to traverse: