cowboysdude Posted September 17, 2017 Share Posted September 17, 2017 I am new to JS but have been trying for three days reading everything and anything I could find...trying new things... I cannot figure out how to parse this in js... It is a valid json file ... it's an object with what looks like arrays in it.. ? Here is part of it... you'll get the idea... { "lang": [{ "code": 1000, "day": "Sunny", "night": "Clear", "icon": 113, "languages": [ { "lang_name": "Chinese Simplified", "lang_iso": "zh", "day_text": "晴天", "night_text": "晴朗" }, { "lang_name": "Finnish", "lang_iso": "fi", "day_text": "Aurinkoinen", "night_text": "Pilvetön" }, { "lang_name": "French", "lang_iso": "fr", "day_text": "Ensoleillé", "night_text": "Clair" }, { "lang_name": "German", "lang_iso": "de", "day_text": "Sonnig", "night_text": "Klar" },{ "lang_name": "Spanish", "lang_iso": "es", "day_text": "Soleado", "night_text": "Despejado" }, { "lang_name": "Swedish", "lang_iso": "sv", "day_text": "Soligt", "night_text": "Klart" }] }, { "code": 1003, "day": "Partly Cloudy", "night": "Partly Cloudy", "icon": 116, "languages": [{ "lang_name": "Chinese Simplified", "lang_iso": "zh", "day_text": "局部多云", "night_text": "局部多云" },{ "lang_name": "Finnish", "lang_iso": "fi", "day_text": "Puolipilvinen", "night_text": "Puolipilvinen" }, { "lang_name": "French", "lang_iso": "fr", "day_text": "Partiellement nuageux", "night_text": "Partiellement nuageux" }, { "lang_name": "German", "lang_iso": "de", "day_text": "leicht bewölkt", "night_text": "leicht bewölkt" }, { "lang_name": "Spanish", "lang_iso": "es", "day_text": "Parcialmente nublado", "night_text": "Parcialmente nublado" }, { "lang_name": "Swedish", "lang_iso": "sv", "day_text": "Växlande molnighet", "night_text": "Växlande molnighet" }] I cannot for the life of me figure out to parse this thing...what I'm after is getting the code ... then the lang_iso so I can show users [who have their language already set] the text in their language... I am probably making this wayyyyyyyyyyyyy too hard but honestly I have tried every loop... every function.... and I cannot get the data I am after.. Help please and Thank you. Link to comment Share on other sites More sharing options...
denno020 Posted September 17, 2017 Share Posted September 17, 2017 Yep this should be pretty easy.. Have you come close to any working code? I wouldn't mind seeing what you've got, and then I can try to show you where you're going wrong and how to change it. Might be better than me just giving you the answer I think Denno Link to comment Share on other sites More sharing options...
cowboysdude Posted September 17, 2017 Author Share Posted September 17, 2017 Well that's what I keep hearing LOL "That's easy..." Like I said I'm sure I'm overthinking this Here is what I've tried: var langFile = this.langFile; var keys = Object.keys(this.langFile); var langFile = this.langFile[keys]; and the standard for (var i=0; i<this.langFile.lang.length; i++) { var langFile = this.langFile.lang[i]; console.log(langFile.languages); } Plus so many other things that I've read on the internet that my head is ready to explode Like I said I'm kinda new to this so be gentle hahaahhha Thank you! Link to comment Share on other sites More sharing options...
denno020 Posted September 17, 2017 Share Posted September 17, 2017 You were on the right path with your for loop.. You just needed to go another level deeper.. I've had to make a few assumptions on what data you know (i.e. what you can test using if conditions), so the code in the attached jsFiddle may be a little off, but it should serve as a decent starting point. Check out what I've written here: var json = { "lang": [{ "code": 1000, "day": "Sunny", "night": "Clear", "icon": 113, "languages": [ { "lang_name": "Chinese Simplified", "lang_iso": "zh", "day_text": "晴天", "night_text": "晴朗" }, { "lang_name": "Finnish", "lang_iso": "fi", "day_text": "Aurinkoinen", "night_text": "Pilvetön" }, { "lang_name": "French", "lang_iso": "fr", "day_text": "Ensoleillé", "night_text": "Clair" }, { "lang_name": "German", "lang_iso": "de", "day_text": "Sonnig", "night_text": "Klar" },{ "lang_name": "Spanish", "lang_iso": "es", "day_text": "Soleado", "night_text": "Despejado" }, { "lang_name": "Swedish", "lang_iso": "sv", "day_text": "Soligt", "night_text": "Klart" }] }, { "code": 1003, "day": "Partly Cloudy", "night": "Partly Cloudy", "icon": 116, "languages": [{ "lang_name": "Chinese Simplified", "lang_iso": "zh", "day_text": "局部多云", "night_text": "局部多云" },{ "lang_name": "Finnish", "lang_iso": "fi", "day_text": "Puolipilvinen", "night_text": "Puolipilvinen" }, { "lang_name": "French", "lang_iso": "fr", "day_text": "Partiellement nuageux", "night_text": "Partiellement nuageux" }, { "lang_name": "German", "lang_iso": "de", "day_text": "leicht bewölkt", "night_text": "leicht bewölkt" }, { "lang_name": "Spanish", "lang_iso": "es", "day_text": "Parcialmente nublado", "night_text": "Parcialmente nublado" }, { "lang_name": "Swedish", "lang_iso": "sv", "day_text": "Växlande molnighet", "night_text": "Växlande molnighet" }] } ]} // Example, get the Spanish translation for Partly Cloudy for (var i = 0 ; i < json.lang.length ; i++) { // Loop the weather types // First iteration will give you 'Sunny' // Second iteration will give you 'Partly Cloudy' // etc var langFile = json.lang[i]; if (langFile.code !== 1003) { // Matching on the code for 'Partly Cloudy' continue; // Continue the loop if the code doesn't match } // Getting here means we know that the variable lang contains the language translations for 'Partly Cloudy' for (var j = 0 ; j < langFile.languages.length ; j++) { // Loop through translations to find Spanish var language = langFile.languages[j]; if (language.lang_iso === 'es') { alert(language.day_text); // Success! This is where you will update your applications text. (You won't use alert) } } } Any questions, let us know . Hope that helps, Denno Link to comment Share on other sites More sharing options...
cowboysdude Posted September 17, 2017 Author Share Posted September 17, 2017 Yes that helps VERY much!! I just adjusted to my vars and bingo..... Well I guess I just learned something new...... and I REALLY appreciate that!!! Thank you so much! Link to comment Share on other sites More sharing options...
denno020 Posted September 17, 2017 Share Posted September 17, 2017 In the interest of completeness, something I didn't add to the jsFiddle is to break the loop(s) once you've found the data you want. Don't need to waste time looping through the rest of the data if you already have your translation value Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.