Reputation: 538
I have this short json file:
{"daten":[
{
"url":"destille",
"lang":"de",
"ausblenden": {
"parts":"farben, sonstiges, material"
}
},
{
"url":"schnaps",
"lang":"de",
"ausblenden": {
"parts":"farben, sonstiges"
}
}
]}
And I want to load just a specific part of the listed data, for example the "schnaps"-part.
$.getJSON('data.json', function(data) {
var output="<ul>";
var uri = "schnaps";
output+="<li>" + data.daten[uri].url + " " + data.daten[uri].ausblenden.parts+"</li>";
output+="</ul>";
document.getElementById("placeholder").innerHTML=output;
});
I have no idea how to get this. loading the whole data is no problem...but just one part seems to be tricky.
Upvotes: 0
Views: 1874
Reputation: 40887
In your JSON
daten
is an array
but schnaps
it is not an index on it. You should index it using a number.
May you can convert your data to:
{
"daten": {
"destille": {
"lang":"de",
"ausblenden": {
"parts":"farben, sonstiges, material"
}
},
"schnaps": {
"lang":"de",
"ausblenden": {
"parts":"farben, sonstiges"
}
}
}
}
Upvotes: 0
Reputation: 38345
In this part:
data.daten[uri].url
daten
refers to an array, and uri
(which is set to "schnaps") would have to be a key in that array, but it's not - "schnaps" is the url
property of an object inside that array.
You'll need to iterate through or otherwise filter the array and obtain the object inside it that has url === "schnaps"
.
Upvotes: 1
Reputation: 382102
If you want to get the object whose url parameter has value "schnaps", you might do this :
$.getJSON('data.json', function(data) {
var objects = data.daten.filter(function(v){return v.url=="schnaps"});
if (objects.length==0) return; // no object has url schnaps
var obj = objects[0];
output+="<li>" + obj.url + ... + obj.ausblenden + ...
...
Upvotes: 2
Reputation: 19153
The problem is you cannot choose your desired element by doing data.daten["schnaps"]
. That datum is in a JSON array, so you should do this instead data.daten[1].url
, which would retrieve the data you want.
Upvotes: 0
Reputation: 2396
Your array is not associative. You should itterate over it like: data.daten[0].url this, because passing a string to the index won't get you anywhere. You could also try searching for other array search mechanisms that would provide you with the functionality you need by searching with a string.
Upvotes: 0