views:

63

answers:

5

Hi Guys,

this is might be a very basic question, but seems like it's not easy to find the answer. I have a Json, more or less is like:

languages = {
"aa":{"iso639-2T":"aar","family":"Afro-Asiatic","labels":{"language_names":["Afar"],"native_names":["Afaraf"]}},
"ab":{"iso639-2T":"abk","family":"Northwest Caucasian","labels":{"language_names":["Abkhaz"],"native_names":["\u0430\u04a7\u0441\u0443\u0430"]}},
"af":{"iso639-2T":"afr","family":"Indo-European","labels":{"language_names":["Afrikaans"],"native_names":["Afrikaans"]}}, etc...etc... }

if you see json above, there's several language object inside languages variable. and each language object has its own name as its identifier ("aa", "ab", "af")

so then, my question is, how to get those identifier ("aa", "ab", "af") if i want to list all of those language in html? eg. if i want to create like a combo box (<option value="aa">Afar</option><option value="ab">Abkhaz</option><option value="af">Afrikaans</option>)

actually what i want to achieve is something like this (in php)

$sampleArray = Array("aa" => "Afar", "ab" => "Abkhaz", "af" => "Afrikaans"); foreach($sampleArray as $id => $value){ /* I can get the id from $id* / }

is there any solution similar like php syntax above for my json in java script?

ps. if you're wondering why i'm not using array - I'm just thinking it will easier to grab certain language object (im just do something like: languages["af"] to get afrikaans language) rather than i should do: loop through the entire language object and check one-by-one if the id is what i want, and then return it. - you can give me another suggestion for this if you guys have a better idea :)

Best Regards,

AnD

+3  A: 

You can use the "for"

for (var key in languages)
{
 alert(key);
}
madsleejensen
+2  A: 
var data = new Array();
for(var lang in languages) {
    data[lang] = languages[lang];
}
mellowsoon
Using `Array` here is wrong. You should use `{}` aka `new Object()`, yes it works with Array too, but that's only because you're setting properties on the Array Object, you're in fact not setting anything IN the Array :)
Ivo Wetzel
@Ivo - I completely agree with you. It just seems like the OP already has what he's looking for in the languages object. So, I figured I'd give him something else.
mellowsoon
I still doesn't get why it's wrong using array?
AnD
What Ivo is saying is.. When you set data["foo"] = "bar", you're not actually adding the key/value "foo" => "bar" to the array. You're actually adding the foo property to the array object, i.e. Array.foo = "bar". While it will work 90% of the time, there are a few gotchas that pop up.
mellowsoon
+2  A: 

Did you try:

languages['af'].labels.language_names[0]

It will return Afrikaans

Mic
+3  A: 

this should do what you want..

to see them

for (var lang in languages)
  alert(lang + ' : ' + languages[lang].labels.language_names[0]);

to put them in the DOM

var $select = $('selector to your select element');
for (var lang in languages)
  $select.append('<option value="'+lang+'">' + languages[lang].labels.language_names[0] + '</option>');

Working code at http://www.jsfiddle.net/EsJAh/

Gaby
`alert(lang + ' : ' + languages[lang].labels.language_names[0]` ?
James Westgate
@james, nice catch .. missed the array there.. fixed in answer.
Gaby
Great! this is exactly what i want :) thanks dude :)
AnD
A: 

If you're using jquery, then you can do this actually:

$.each( languages, function(k, v){
        alert( "Key: " + k + ", Value: " + v );
        // value is your particular language object based on k variable
});

:)

AnD