[jQuery] Re: Some jQuery Questions
On Aug 31, 5:26 pm, "Michael Geary" <[EMAIL PROTECTED]> wrote: > > Mark Gibson wrote a lightweight jQuery plugin with $.toJSON() and > > $.parseJSON(): > >http://jollytoad.googlepages.com/json.js > > Actually, Douglas "JSON" Crockford wrote the original code, so it's been > well tested and proven: > > http://www.json.org/js.html > > Unfortunately, Doug's original code adds the toJSON and parseJSON to > Object.prototype, which breaks jQuery and a lot of other code. Mark changed > them into standalone functions (in the jQuery/$ namespace), making the code > compatible with jQuery. (Thanks, Mark!) Actually, we're currently using Crockford's json.js script as well as a couple of XML and JSON helper objects by Yusuke Kawasaki[1]. We did have to go back and fix all of our object iterators, inserting .hasOwnProperty() wherever necessary. We're enriching the client-side experience moving forward, though (yay!), so Mark's code is something on my "Implement Post Haste" list after we officially include our selected toolkits (currently MS Ajax and jQuery). Pyro [1] http://www.kawa.net/xp/index-e.html
[jQuery] Re: Some jQuery Questions
> > > Here is a example JSON: > > > > > > var json = { > > > fields: > > > [ > > >{prompt1: "Login Name"}, > > >{prompt2: "Real Name"}, > > >{prompt3: "Location"}, > > >{prompt4: "Password"}, > > >{prompt5: "Security Group"}, > > >{prompt6: "File Area"} > > > ] > > > }; > > That's not valid JSON. You need double quotes on all the > > property names. > > > > Obviously it doesn't make any difference if you are just > > eval'ing it (it is valid JavaScript), but if anyone uses it with > > a real JSON parser it will fail. > Ok, so what you are saying that I can make it work fine for > my own I/O handling, but to be 100% correct across the board, > the BCP (best current practice) is I should always have my > JSON generation code doube quote all JSON properties and I > will not have any compatibility issue? or shouldn't? > > Now, must it be double quote and/or single quotes? Does it matter? It must be double quotes. It's not a question of best current practice, it's a question of whether it's JSON or not. No double quotes = not JSON. > I think single quote is often used for WEB work because many > back end languages only support double quotes for strings so > its easy to code it. So I have been using single quotes up > to this point and double quote it where it is specifically required. That's totally reasonable, and at least 90% of the "JSON" data in the world is probably consumed by non-strict JavaScript parsers, most often a simple eval() which will accept any valid JavaScript code whether it is JSON or not. But, to make it through any strict JSON parser the double quotes are required. Here is a JSON parser and some more information about this requirement: http://simonwillison.net/2006/Oct/11/json/ -Mike
[jQuery] Re: Some jQuery Questions
On Aug 31, 4:51 pm, "Michael Geary" <[EMAIL PROTECTED]> wrote: > > Here is a example JSON: > > > var json = { > > fields: > > [ > >{prompt1: "Login Name"}, > >{prompt2: "Real Name"}, > >{prompt3: "Location"}, > >{prompt4: "Password"}, > >{prompt5: "Security Group"}, > >{prompt6: "File Area"} > > ] > > }; > > That's not valid JSON. You need double quotes on all the property names. > > Obviously it doesn't make any difference if you are just eval'ing it (it is > valid JavaScript), but if anyone uses it with a real JSON parser it will > fail. I wanted to get back to you on this. Ok, so what you are saying that I can make it work fine for my own I/O handling, but to be 100% correct across the board, the BCP (best current practice) is I should always have my JSON generation code doube quote all JSON properties and I will not have any compatibility issue? or shouldn't? Now, must it be double quote and/or single quotes? Does it matter? I think single quote is often used for WEB work because many back end languages only support double quotes for strings so its easy to code it. So I have been using single quotes up to this point and double quote it where it is specifically required. -- HLS
[jQuery] Re: Some jQuery Questions
On Aug 31, 2:37 pm, "John Beppu" <[EMAIL PROTECTED]> wrote: > > 2) Best way to create a toJSON() string or object. > > anyObject.toSource() Hi John, Yes, at first, that is what I thought and at first, i said "Ah ha!, thats it!" But its not quite right for all situations and worst, atleast up to IE 6.0 it doesn't support this JavaScript String object method. So you will get a script error under IE. :-) I have not tried it update IE 7.0 --- HLS
[jQuery] Re: Some jQuery Questions
Michael Geary wrote: > > IE doesn't like: > > > > json = {}; > > > > but will accept: > > > >var json = {}; > > Let me take a guess... > > You are executing this code inside a function, and you have an HTML element > in your page with the id 'json'. > ... > Did I get it right? Gawd, I should of seen that. :-)It only came up because I had temporary changed it to a global scope so that I can see it under firebug. It was a local scope in the working code. :-) > > Also, I did try the original json.js from json.org and there > > I learned that I needed to understand another concept - > > pulling just the array from a jQuery result. This is because > > the JSON parser was creating json for all the properties. I > > then realized the jQuery .get() method purpose in life. > > That's true, .get() does return a full-blooded Array object (unlike the > jQuery result object which does have numbered elements and a .length > property but is not a true Array). > > But I don't see how that helps you with JSON. The jQuery result object is an > array of DOM elements, and there is no JSON representation for those. So you > can't directly convert a jQuery result object into any kind of JSON format. Well, this is all among those things where like a scrambled puzzle, I am getting all my ducks line up to see how to do put many things together. For example, going from a string to object and back. concatenation, merging and unraveling. Alot of stuff that I still need to understand without "3rd party" libraries. :-) Thanks
[jQuery] Re: Some jQuery Questions
> > > From: Pops > > > I figured it out know. You got to look at the > > > constructor type to see if its an Object, Array > > > or String. From there you can decide to use > > > for each or for in or for loop. > > From: Michael Geary > > You don't have to write that code yourself: > > http://jollytoad.googlepages.com/json.js > Yeah sure. but 90% of the battle is always understanding > other people's code. For me to master a language (in this > case JS/jQuery), I have to roll my own, trial and error, live > and learn, understanding the basis and then I would be able > to say "a, ok, I can understand how this.js or that.js > relates and how I can use it or not use it. > Too much overhead". Etc You're a lot like me that way. And it's not that much work to build a simple JSON serializer. But as you can see from json.js, there are just enough weird little cases to make it "interesting". :-) > IE doesn't like: > > json = {}; > > but will accept: > >var json = {}; Let me take a guess... You are executing this code inside a function, and you have an HTML element in your page with the id 'json'. Unlike other browsers, IE creates a read-only property of the window object for every id in your page. If you have an element whose ID is 'json', then window.json is a reference to that object. "var json = {}" succeeds because it creates a local variable inside the function named 'json', so there is no conflict. "json = {}" fails because it attempts to create a window.json property, but that property already exists and is read-only. If you were to execute "var json = {}" in the global scope (outside any function), it would fail too. Did I get it right? > Also, I did try the original json.js from json.org and there > I learned that I needed to understand another concept - > pulling just the array from a jQuery result. This is because > the JSON parser was creating json for all the properties. I > then realized the jQuery .get() method purpose in life. That's true, .get() does return a full-blooded Array object (unlike the jQuery result object which does have numbered elements and a .length property but is not a true Array). But I don't see how that helps you with JSON. The jQuery result object is an array of DOM elements, and there is no JSON representation for those. So you can't directly convert a jQuery result object into any kind of JSON format. -Mike
[jQuery] Re: Some jQuery Questions
> From: Pyrolupus > > Mark Gibson wrote a lightweight jQuery plugin with $.toJSON() and > $.parseJSON(): > > http://jollytoad.googlepages.com/json.js > > I haven't tried it out, yet, but it looked good at a cursory glance. Actually, Douglas "JSON" Crockford wrote the original code, so it's been well tested and proven: http://www.json.org/js.html Unfortunately, Doug's original code adds the toJSON and parseJSON to Object.prototype, which breaks jQuery and a lot of other code. Mark changed them into standalone functions (in the jQuery/$ namespace), making the code compatible with jQuery. (Thanks, Mark!) -Mike
[jQuery] Re: Some jQuery Questions
Michael Geary wrote: > > I figured it out know. You got to look at the constructor type to see > > if its an Object, Array or String. From there you can decide to use > > for each or for in or for loop. > > You don't have to write that code yourself: > > http://jollytoad.googlepages.com/json.js Yeah sure. but 90% of the battle is always understanding other people's code. For me to master a language (in this case JS/jQuery), I have to roll my own, trial and error, live and learn, understanding the basis and then I would be able to say "a, ok, I can understand how this.js or that.js relates and how I can use it or not use it. Too much overhead". etc > Description from the plugins page (http://docs.jquery.com/Plugins): > > hacked the original json.js into a jQuery plugin. It adds the two > functions:$.toJSON(value),$.parseJSON(json_str, [safe]). > > See the code for more details. Thanks I will. More to my point above. IE doesn't like: json = {}; but will accept: var json = {}; Also, I did try the original json.js from json.org and there I learned that I needed to understand another concept - pulling just the array from a jQuery result. This is because the JSON parser was creating json for all the properties. I then realized the jQuery .get() method purpose in life. Thanks mike.
[jQuery] Re: Some jQuery Questions
> Here is a example JSON: > > var json = { > fields: > [ >{prompt1: "Login Name"}, >{prompt2: "Real Name"}, >{prompt3: "Location"}, >{prompt4: "Password"}, >{prompt5: "Security Group"}, >{prompt6: "File Area"} > ] > }; That's not valid JSON. You need double quotes on all the property names. Obviously it doesn't make any difference if you are just eval'ing it (it is valid JavaScript), but if anyone uses it with a real JSON parser it will fail. > I figured it out know. You got to look at the constructor type to see > if its an Object, Array or String. From there you can decide to use > for each or for in or for loop. You don't have to write that code yourself: http://jollytoad.googlepages.com/json.js Description from the plugins page (http://docs.jquery.com/Plugins): hacked the original json.js into a jQuery plugin. It adds the two functions:$.toJSON(value),$.parseJSON(json_str, [safe]). See the code for more details. -Mike
[jQuery] Re: Some jQuery Questions
> 2) Best way to create a toJSON() string or object. > I didn't see a direct way to do this, if not, is this best way: >// Create JSON from form input id="wc*" fields > >$jj = $('[EMAIL PROTECTED]').get(); >json = {}; >try { > for (var j=0; j <$jj.length; j++) { > json[$jj[j].getAttribute("name")] = $jj[j].value; > } >} catch(e) { > alert("EXCEPTION:\n\n"+e); > return false; >} You need to recurse so that you can get subobjects-arrays. Mark Gibson wrote a lightweight jQuery plugin with $.toJSON() and $.parseJSON(): http://jollytoad.googlepages.com/json.js I haven't tried it out, yet, but it looked good at a cursory glance. Pyro
[jQuery] Re: Some jQuery Questions
> > 2) Best way to create a toJSON() string or object. anyObject.toSource()
[jQuery] Re: Some jQuery Questions
Klaus Hartl wrote: > Yes, there is a direct method, just use a for loop: > > for (var key in json) { > console.log(key); // key > console.log(json[key]); // value > } > > Maybe I didn't get the question right? Hi Klaus, Ok, Now I see why I asked the question. Yes, I did try the above, but I see in some code snippet I was struggling to grasp JSON Objects, Strings and Arrays: Here is a example JSON: var json = { fields: [ {prompt1: "Login Name"}, {prompt2: "Real Name"}, {prompt3: "Location"}, {prompt4: "Password"}, {prompt5: "Security Group"}, {prompt6: "File Area"} ] }; Essentially, I wanted to pull the property names ("prompt1" to "prompt6"). They don't need to have to this pattern in my case. The prompts can have varying names. So when I used the "for in" syntax above, it wasn't quite what I wanted. For example: for (var i in json) { console.log(i, " | ",json[i]); for (var k in json[i]) { console.log(k," | ",json[i][k]); } } Produces this: fields | [Object prompt1=Login Name, Object prompt2=Real Name, Object prompt3=Location, Object prompt4=Password, Object prompt5=Security Group, Object prompt6=File Area] 0 | Object prompt1=Login Name 1 | Object prompt2=Real Name 2 | Object prompt3=Location 3 | Object prompt4=Password 4 | Object prompt5=Security Group 5 | Object prompt6=File Area I figured it out know. You got to look at the constructor type to see if its an Object, Array or String. From there you can decide to use for each or for in or for loop. I just figured that with all the "selector" stuff, that they would be a more direct way. :-) -- HLS
[jQuery] Re: Some jQuery Questions
Klaus Hartl wrote: > I'm going to answer the first question (low hanging fruit)... :-) > Yes, there is a direct method, just use a for loop: > > for (var key in json) { > console.log(key); // key > console.log(json[key]); // value > } > > Maybe I didn't get the question right? I don't know why I missed this. I think at the moment I was mentally stuck on using "for each" and stuck grasping and learning direct property references. Thanks
[jQuery] Re: Some jQuery Questions
Pops wrote: Now that getting a handle on the JS/jQuery language, I have these basic questions 1) For an JSON object, how do you get the key name? Example var json = {field1: "data2", field2: "data2"}; I know how to get the data, but how do you get the field names? IOW, I want to get the names of the keys. Is there a method that traverses the JSON and returns key/value? Do I need regex() or is there a more direct method? I'm going to answer the first question (low hanging fruit)... Yes, there is a direct method, just use a for loop: for (var key in json) { console.log(key); // key console.log(json[key]); // value } http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Statements:for...in Maybe I didn't get the question right? --Klaus