Parrotmaster
Parrotmaster

Reputation: 676

Read values from JSON object

I am currently trying to get contents from a JSON object. I've been looking all over google and what I think should work, for some reason, does not.

The JSON looks something like this:

{"locatie0":{"naam0":["Domplein"],"value0":["value1"]},"locatie1":{"naam1":["Neude"],"value1":["value1"]},"locatie2":{"naam2":["Vredenburg"],"value2":["value1"]},"locatie3":{"naam3":["Stadhuisbrug"],"value3":["value1"]},"locatie4":{"naam4":["Korte Minrebroederstraat"],"value4":["value1"]}}

I use below code to read the file but for some reason it will always return undefined or NaN.

$.ajax({
    url: "_data/pleinen.json",
    type: 'get', 
    dataType: "json", 
    success: function(data) {
        for(var k = 0; k < _loc.length; k += 1) {
            var _locaties = data.locatie[k].naam[k];
            // Should alert "Domplein", "Neude", "Vredenburg", "Stadhuisbrug", "Korte Minrebroekstraat", 
            alert(_locaties);
        }
    }
});

Can anybody see if i've made any mistakes in my code or if there's a better way to read the values?

Upvotes: 0

Views: 8929

Answers (5)

Vassilis Barzokas
Vassilis Barzokas

Reputation: 3242

The locatie and the naam are not arrays, so you cannot access them like the way you do.

You have to combine the number with the name as a string. Something like that

data['locatie'+k]

Upvotes: 2

Felix Kling
Felix Kling

Reputation: 816522

locatie[k] tries to access the property k of the object in locatie, but in your case, the number is part of the name itself. You have to build the property name dynamically. In addition, each property of the nested objects is an array with one element, so you have to access the first element:

var _locaties = data['locatie' + k]['naam' + k][0];

Your data structure is a bit strange though. The numbers in the property names makes accessing the data more difficult. If you can change it, change it to an array of objects and don't use arrays for the properties if you don't need them:

[{"naam": "Domplein", "value": "value1"}, {"naam": "Neude", "value":"value1"}]

Then accessing the data is simply:

for (var i = 0, l = data.length; i < l; i++) {
    var _locaties = data[i].naam;
}

Upvotes: 7

Vadim
Vadim

Reputation: 8789

Try this:

$.ajax({
    url: "_data/pleinen.json",
    type: 'get', 
    dataType: "json", 
    success: function(data) {
        for(var k = 0; k < _loc.length; k += 1) {
            var _locaties = data['locatie'+k]['naam'+k][0]; 
            alert(_locaties);
        }
    }
});

As the value of naam property is array you need to fetch first item from it in order to get string value.

Upvotes: 2

loxxy
loxxy

Reputation: 13151

A JSON does not guarantee any order like an array.

But in your case, the keys of the JSON have index hints, so try accessing "naam" with:

 data["locatie" + k]["naam" + k]

Upvotes: 2

rab
rab

Reputation: 4144

try this ;

k = 0;
console.log( 
    data[ 'locatie'+ k]['naam'+ k][0], 
    data[ 'locatie'+ k]['value'+ k ][0]
);

k = 1;
console.log( 
    data[ 'locatie'+ k]['naam'+ k][0], 
    data[ 'locatie'+ k]['value'+ k ][0]
);

Upvotes: 1

Related Questions