Matej Posted October 4, 2014 Share Posted October 4, 2014 (edited) Hello, is there any way to invoke json attribute like array or with variable? For example: var x = [ {1:"matej"}, {1:"adam"}, {1:"clyde"}, {1:"jozko"}, {1:"matej"}, {1:"jude"}, {1:"john"}, {1:"marek"}, {1:"juraj"}, {1:"alex",2:"alojz",3:"idiot",4:"RESULT"}, {1:"matej"}, {1:"andrek"}, {1:"matej"}, {1:"brano"} ];var date=new Date().getMonth(); // 10var attribute=new Date()-getDay()-2; //4alert(x[date].attribute); // basicly i want it to alert "RESULT" , but this isnt working , i want to invoke lile this bcs i want it to change automaticly every day I hope i made this clear , this is basicly something like on school sites which writes who has birthday every day. Thanks for answers Edited October 4, 2014 by Matej Link to comment Share on other sites More sharing options...
Ingolme Posted October 4, 2014 Share Posted October 4, 2014 It would be x[date][attribute] Objects can be accessed in the same way arrays are: object.property = 5; or object["property"] = 5; or x = "property"; object[x] = 5; Link to comment Share on other sites More sharing options...
Matej Posted October 4, 2014 Author Share Posted October 4, 2014 thanks for answers x[date][attribute] does not work but i made it like this var datum = new Date();var mesiac = datum.getMonth();var den = datum.getDay() - 2;var zisti=x[mesiac];document.getElementById("lol").innerHTML="Today is " +zisti[den]+"'s"+" namesday"; Link to comment Share on other sites More sharing options...
davej Posted October 4, 2014 Share Posted October 4, 2014 I'm not sure why you are calling this JSON. This is just an example which uses an array of objects. JSON means that you have formatted the object more strictly with double-quotes around keys and strings (as I have done below). This strict format is not actually necessary for ordinary Javascript objects, and is not actually necessary below. <!DOCTYPE html><html lang="en"><head><meta charset="utf-8"/><title>title</title><style></style><script>window.onerror = function(m, u, l){alert('Javascript Error: '+m+'nURL: '+u+'nLine Number: '+l);return true;}</script><script>window.onload = init;function init() {document.getElementById('btn1').onclick = put;}function put(){var str = "";var list1 = [ {"name": "Fred", "bday": "10/4/2002"}, {"name": "George", "bday": "10/4/2006"}, {"name": "Martha", "bday": "10/4/2008"}, {"name": "Billy", "bday": "10/4/2005"}, ];var dt = new Date();var m = dt.getMonth() + 1;var d = dt.getDate();var y = dt.getFullYear();var bdays;for (var i=0,len=list1.length ; i<len ; i++){bstr = list1[i].bday.split('/');if( (bstr[0] == m) && (bstr[1] == d) ){str += list1[i].name +' is '+ (y - bstr[2]) +' today<br/>';} }//end fordocument.getElementById("out").innerHTML = str;}</script></head><body><input type="button" id="btn1" value="Enter"><div id="out"></div></body> </html> Link to comment Share on other sites More sharing options...
thescientist Posted October 6, 2014 Share Posted October 6, 2014 The full details behind JSON and what constitutes JSON can be found here. http://json.org/ Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now