/    Sign up×
Community /Pin to ProfileBookmark

JSON reference examples

I have come up with 4 ways to access the contents of a JSON object.

Are there any other ways available?
Would a JSON.stringify or JSON.parse approach be better?
Should I avoid the use of the eval() technique completely?

Here is my code:

[code]

<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8″ />
<title> JSON Object Reference </title>
<style>
fieldset { width: 20%; float: left; }
#fs1 { background-color: pink; }
#fs2 { background-color: lime; }
#fs3 { background-color: yellow; }
#fs4 { background-color: orange; }
</style>
</head>

<body>
<fieldset> <legend>Rolls (Direct access)</legend> <pre id=”fs1″></pre> </fieldset>
<fieldset> <legend>Rolls (Access via function)</legend> <pre id=”fs2″></pre> </fieldset>
<fieldset> <legend>Rolls (Access via array)</legend> <pre id=”fs3″></pre> </fieldset>
<fieldset> <legend>Rolls (Access via eval)</legend> <pre id=”fs4″></pre> </fieldset>

<script>
function doc(IDS) { return document.getElementById(IDS); }

Rolls = {
FRR33:’0:FRR33 3 2 1 5 1 2 3 1′, // FRR33
FRR43:’0:FRR43 4 2 1 5 1 2 3 1′, // FRR43
FRR44:’0:FRR44 4 2 1 5 1 2 4 1′, // FRR44
FR33: ‘0:FR33 3 2 1 3 2 1 5 1’, // FR33
FR34: ‘0:FR34 3 2 1 4 2 1 5 1’, // FR34
FR44: ‘0:FR44 4 2 1 4 2 1 5 1’ // FR44
};

/* ——————–Direct Access————————————- */
doc(‘fs1′).innerHTML += Rolls.FRR33+’n’;
doc(‘fs1′).innerHTML += Rolls.FRR43+’n’;
doc(‘fs1′).innerHTML += Rolls.FRR44+’n’;
doc(‘fs1′).innerHTML += Rolls.FR33+’n’;
doc(‘fs1′).innerHTML += Rolls.FR34+’n’;
doc(‘fs1′).innerHTML += Rolls.FR44+’n’;

/* ——————–Access via function——————————- */
function getObj(info) { return info; } // Rolls.info;

doc(‘fs2′).innerHTML += getObj(Rolls.FRR33)+’n’;
doc(‘fs2′).innerHTML += getObj(Rolls.FRR43)+’n’;
doc(‘fs2′).innerHTML += getObj(Rolls.FRR44)+’n’;
doc(‘fs2′).innerHTML += getObj(Rolls.FR33)+’n’;
doc(‘fs2′).innerHTML += getObj(Rolls.FR34)+’n’;
doc(‘fs2′).innerHTML += getObj(Rolls.FR44)+’n’;

/* ——————–Access via array———-(similar to above)—— */
var seqObj = [Rolls.FRR33, Rolls.FRR43, Rolls.FRR44, Rolls.FR33, Rolls.FR34, Rolls.FR44];
for (var i=0; i<seqObj.length; i++) {
doc(‘fs3′).innerHTML += getObj(seqObj[i])+’n’;
}

/* ——————–Access via eval———————————– */
function getRoll(info) { return eval(‘Rolls.’+info); }
var seqVar = [‘FRR33’, ‘FRR43’, ‘FRR44’, ‘FR33’, ‘FR34’, ‘FR44’ ];
for (var i=0; i<seqVar.length; i++) {
doc(‘fs4′).innerHTML += getRoll(seqVar[i])+’n’;
}

/* ——————–Alternative methods???—————————- */
</script>

</body>
</html>

[/code]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@rootApr 01.2018 — eval should only be used as a last resort, there are valid uses for it and specific uses for it too but generally if you resort to using it without researching if its the right approach, your considered a very poor and sloppy programmer in certain programming circles, so please do avoid using it.

JSON.pase is the turning a STRING that JSON.stringify made or the PHP server side version made... in to an object that you can use.

example... Rolls is a variable and treated like an object, so

Rolls[info] will return the field you were seeking
function getRoll(info) { return Rolls[info]; } will return the desired data, as long as it exists.
Copy linkTweet thisAlerts:
@JMRKERauthorApr 02.2018 — Thanks 'root' (formally known as . ? )

That function is certainly easier to write than what I was using

and gives the same result as the eval().

Question: Since the 'Roll' patterns will be unchanging

should I declare with a 'const' rather than a 'var'?
Copy linkTweet thisAlerts:
@rootApr 02.2018 — TBH ... I am not a fan of this surge in changes to JavaScript, it was and always has meant to be an open and flexible language but the stuffed shirts are trying to put constraints on how easy it will be to code using JavaScript by imposing such stupidity as "strict"; mode and [COLOR="#000080"]let[/COLOR] and [COLOR="#000080"]const[/COLOR], sure, if you want a constant, I can understand that need but in a language such as JavaScript in the web browser, these changes are overkill.

[B][COLOR="#000080"]var[/COLOR][/B] only makes the variable available within the scope it was initiated, handy in a function that you want to stop external interference from outside sources, but over use of it, not good IMHO as some variables will always have to be in the global scope.
×

Success!

Help @JMRKER spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...