Json name value pairs
-
Yes that's very good but I need to keep everything in js and just use Ruby to manipulate the SU display.
-
Ah! I didn't realise you where tallking about JS.
-
@chrisglasier said:
I need to keep everything in js
JSON is JavaScript. Given that x is a valid JSON object,
eval( 'foo =' + x )
is all you need. -
@martinrinehart said:
@chrisglasier said:
I need to keep everything in js
JSON is JavaScript. Given that x is a valid JSON object,
eval( 'foo =' + x )
is all you need.Nonsense. Here is the code I finally cobbled together from posts by people who know about these things at the CodingForum:
var r = [] , i = -1; for (var z in ob) { if (ob.hasOwnProperty(z)) { i++; if(isNaN(ob[z][0]) == false) r[i] = [ob[z],z]; //number arrays else r[i] = [z , ob[z]]; }; }; return r;
-
@chrisglasier said:
@martinrinehart said:
JSON is JavaScript. Given that x is a valid JSON object,
eval( 'foo =' + x )
is all you need.Nonsense.
Chris, that's most unkind. Take two Doug Crockfords and get back to me with a counter example.
-
@martinrinehart said:
Chris, that's most unkind. Take two Doug Crockfords and get back to me with a counter example.
Sorry, maybe I've missed something but please explain evaluating a string + an object. What I have shown before works. It gives me the name value pairs as the caption.
Doug Crockfords are biscuits like Prince Charles's Duchy Originals?
Happy new year!
Chris
-
var db = { "projects"; [ { "type"; "Head", "genus"; "Project", "name"; "House01", "link"; [3], "all"; [5,6] }, { "type"; "Head", "genus"; "Project", "name"; "House02", "link"; [3], "all"; [5,6] } ] } eval(db) for (x in db.projects) { alert(db.projects[x].name) }
Doug Crockford is the inventor of JSON. take a look also on javascript the good parts
check this fun:
alert(0.1+0.2) => 0.30000000000000004
-
-
@thomthom said:
wtf?
if it is floats involved dont trust anyone
alert((0.1+0.2).toPrecision(2)) => 0.30
-
@unknownuser said:
Doug Crockford is the inventor of JSON. take a look also on javascript the good parts
Didn't know he was on youtube, but the two Dougs I had in mind were JSON.org and the book JavaScript-The Good Parts.
JSON, Chris, was not defined by Crockford; it was discovered by Crockford lurking inside Eich's sometimes quirky, sometimes brilliant language. JSON is, by its definition, the syntax JavaScript uses to describe objects. Therefore
eval( 'foo = ' + json );
is valid JavaScript.
Oddly, my first attempt at this failed. I tried:
foo = eval( json );
If you got it running without
eval()
in anything like a general way you've done great work. Crockford is also famous for saying "eval()
is evil". -
-
@unknownuser said:
Doug Crockford is the inventor of JSON.
Yes yes ... I was just making sure two nonsenses don't make a sense.
-
@chrisglasier said:
@unknownuser said:
Doug Crockford is the inventor of JSON.
Yes yes ... I was just making sure two nonsenses don't make a sense.
2(-1) = -2
so
2(nonsense) = non-twosensesbut:
(-1)^2 = 1
so
nonsense^2 = a senseit follows then that:
the squareroot of nonsense must be imaginary -
@martinrinehart said:
.... JSON is, by its definition, the syntax JavaScript uses to describe objects. Therefore
eval( 'foo = ' + json );
is valid JavaScript.
Oddly, my first attempt at this failed. I tried:
foo = eval( json );
If you got it running without
eval()
in anything like a general way you've done great work.If you say given 'that json is a valid JSON object' it means to my novice ears that the text written using jsonotation has already been eval'd or parsed.
In my case it gets parsed when it is imported as an object from a text file.
That's all!
-
Right - eval gets passed String objects. So before the eval is called,
json
is a variable which references a String.After the eval, the variable
foo
refers to a Javascript Object.However,
@unknownuser said:
To convert a JSON text into an object, you can use the eval() function. eval() invokes the JavaScript compiler. Since JSON is a proper subset of JavaScript, the compiler will correctly parse the text and produce an object structure. The text must be wrapped in parens to avoid tripping on an ambiguity in JavaScript's syntax.
var myObject = eval('(' + myJSONtext + ')');
and
@unknownuser said:
..If the server is not rigorous in its JSON encoding, or if it does not scrupulously validate all of its inputs, then it could deliver invalid JSON text that could be carrying dangerous script. The eval function would execute the script, unleashing its malice.
To defend against this, a JSON parser should be used. A JSON parser will recognize only JSON text, rejecting all scripts. In browsers that provide native JSON support, JSON parsers are also much faster than eval. It is expected that native JSON support will be included in the next ECMAScript standard.
var myObject = JSON.parse(myJSONtext, reviver);
I believe Chris is using json2.js on my recommendation, which is based on DC's recommendation because it is possible CG's data could come from an outside source.
And I would also repeat what TBD said and watch the "Good Stuff" video - at least you will know what to avoid in Javascript to make your life easier.
Advertisement