sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Json name value pairs

    Scheduled Pinned Locked Moved Developers' Forum
    19 Posts 6 Posters 1.1k Views 6 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • thomthomT Offline
      thomthom
      last edited by

      Hash.keys - return array of keys: http://ruby-doc.org/core/classes/Hash.html#M002866

      And when you iterate:

      myHash.each { |key, value|
        puts "Key; #{key} - Value; #{value}"
      }
      

      Thomas Thomassen β€” SketchUp Monkey & Coding addict
      List of my plugins and link to the CookieWare fund

      1 Reply Last reply Reply Quote 0
      • chrisglasierC Offline
        chrisglasier
        last edited by

        Yes that's very good but I need to keep everything in js and just use Ruby to manipulate the SU display.

        With TBA interfaces we can analyse what is to be achieved so that IT can help with automation to achieve it.

        1 Reply Last reply Reply Quote 0
        • thomthomT Offline
          thomthom
          last edited by

          Ah! I didn't realise you where tallking about JS.

          Thomas Thomassen β€” SketchUp Monkey & Coding addict
          List of my plugins and link to the CookieWare fund

          1 Reply Last reply Reply Quote 0
          • M Offline
            MartinRinehart
            last edited by

            @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.

            Author, Edges to Rubies - The Complete SketchUp Tutorial at http://www.MartinRinehart.com/models/tutorial.

            1 Reply Last reply Reply Quote 0
            • chrisglasierC Offline
              chrisglasier
              last edited by

              @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;
              
              

              With TBA interfaces we can analyse what is to be achieved so that IT can help with automation to achieve it.

              1 Reply Last reply Reply Quote 0
              • M Offline
                MartinRinehart
                last edited by

                @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.

                Author, Edges to Rubies - The Complete SketchUp Tutorial at http://www.MartinRinehart.com/models/tutorial.

                1 Reply Last reply Reply Quote 0
                • chrisglasierC Offline
                  chrisglasier
                  last edited by

                  @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

                  With TBA interfaces we can analyse what is to be achieved so that IT can help with automation to achieve it.

                  1 Reply Last reply Reply Quote 0
                  • tbdT Offline
                    tbd
                    last edited by

                    
                    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
                    

                    SketchUp Ruby Consultant | Podium 1.x developer
                    http://plugins.ro

                    1 Reply Last reply Reply Quote 0
                    • thomthomT Offline
                      thomthom
                      last edited by

                      @unknownuser said:

                      check this fun:

                      alert(0.1+0.2) 
                      > => 0.30000000000000004
                      

                      wtf? 😲

                      Thomas Thomassen β€” SketchUp Monkey & Coding addict
                      List of my plugins and link to the CookieWare fund

                      1 Reply Last reply Reply Quote 0
                      • tbdT Offline
                        tbd
                        last edited by

                        @thomthom said:

                        wtf? 😲

                        if it is floats involved dont trust anyone πŸ˜‰

                        alert((0.1+0.2).toPrecision(2))
                        => 0.30  
                        

                        SketchUp Ruby Consultant | Podium 1.x developer
                        http://plugins.ro

                        1 Reply Last reply Reply Quote 0
                        • M Offline
                          MartinRinehart
                          last edited by

                          @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".

                          Author, Edges to Rubies - The Complete SketchUp Tutorial at http://www.MartinRinehart.com/models/tutorial.

                          1 Reply Last reply Reply Quote 0
                          • M Offline
                            MartinRinehart
                            last edited by

                            @thomthom said:

                            wtf? 😲

                            Your complete fluency in my native tongue never ceases to amaze me!

                            Author, Edges to Rubies - The Complete SketchUp Tutorial at http://www.MartinRinehart.com/models/tutorial.

                            1 Reply Last reply Reply Quote 0
                            • chrisglasierC Offline
                              chrisglasier
                              last edited by

                              @unknownuser said:

                              Doug Crockford is the inventor of JSON.

                              Yes yes ... I was just making sure two nonsenses don't make a sense.

                              With TBA interfaces we can analyse what is to be achieved so that IT can help with automation to achieve it.

                              1 Reply Last reply Reply Quote 0
                              • Dan RathbunD Offline
                                Dan Rathbun
                                last edited by

                                @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-twosenses

                                but:
                                (-1)^2 = 1
                                so
                                nonsense^2 = a sense

                                it follows then that:
                                the squareroot of nonsense must be imaginary

                                I'm not here much anymore.

                                1 Reply Last reply Reply Quote 0
                                • chrisglasierC Offline
                                  chrisglasier
                                  last edited by

                                  @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!

                                  With TBA interfaces we can analyse what is to be achieved so that IT can help with automation to achieve it.

                                  1 Reply Last reply Reply Quote 0
                                  • J Offline
                                    Jim
                                    last edited by

                                    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.

                                    Hi

                                    1 Reply Last reply Reply Quote 0
                                    • 1 / 1
                                    • First post
                                      Last post
                                    Buy SketchPlus
                                    Buy SUbD
                                    Buy WrapR
                                    Buy eBook
                                    Buy Modelur
                                    Buy Vertex Tools
                                    Buy SketchCuisine
                                    Buy FormFonts

                                    Advertisement