|
Prev: Script Debugging: Firebug for Firefox 3.0 (was: <OT> Set a GuinnessWorld Record with new Firefox 3.0 </OT>)
Next: Need Help with Calculator Java Script
From: SirCodesALot on 24 Jun 2008 12:02 Hi All, Is it possible to create an JSON object from a string? For example var myArray = new Array(10) for (var i=0;i<10;i++) { myArray.push("{idx:"+i+",val:"+i+"}"); } var myStrObj = "[" + myArray.join(",") + "]"; var options = {"val1": "test", "val2" : myStrObj} I would like val2 to be an array of the values from myArray. is this possible, or do i have to break it out manually? Thanks in advance for your suggestions. -Scott
From: Peter Michaux on 24 Jun 2008 12:28 On Jun 24, 9:02 am, SirCodesALot <sjour...(a)gmail.com> wrote: > Hi All, > > Is it possible to create an JSON object from a string? For example > > var myArray = new Array(10) > > for (var i=0;i<10;i++) > { > myArray.push("{idx:"+i+",val:"+i+"}"); > > } > > var myStrObj = "[" + myArray.join(",") + "]"; > > var options = {"val1": "test", > "val2" : myStrObj} > > I would like val2 to be an array of the values from myArray. is this > possible, or do i have to break it out manually? Hand building JSON is really not a great idea. It is very error prone.. It is better to build up a JavaScript object and then use a JSON dumper function http://www.json.org/json2.js var options = {val1:"test", val2:[{idx:0, val:0},{idx:1, val:1}]}; JSON.stringify(options); Peter
From: Thomas 'PointedEars' Lahn on 24 Jun 2008 12:41 SirCodesALot wrote: > Is it possible to create an JSON object from a string? You really don't mean "JSON object" here. If there would be such a thing, it would be an object that is referenced by the property with the name `JSON'. Remember that JSON is a *data exchange format*, not a framework by itself. The object that results from parsing JSON is a native user-defined ECMAScript (Object or Array) object. > For example > > var myArray = new Array(10) Passing a number argument in order to initialize the array is unnecessary, as ECMAScript arrays are of variable length. It is also error-prone, as implementations differ: in the next-to-worst case you will end up with an array that has the number value `10' as its first element. Use var myArray = []; instead, it can be considered to be safe although not universally supported (JavaScript 1.3+, JScript 2.0+, ECMAScript 3). > for (var i=0;i<10;i++) > { > myArray.push("{idx:"+i+",val:"+i+"}"); > } Note that Array.prototype.push() requires at least JavaScript 1.2 and, more important, JScript 5.5. You may want to augment Array.prototype or the Array object itself with such a method. (If you choose to augment the Array.prototype object, you should also provide an iterator method that handles the side-effects of the augmentation.) <http://PointedEars.de/es-matrix/> Also, this only works as long as `i' is not an arbitrary string value. > var myStrObj = "[" + myArray.join(",") + "]"; Array objects inherit a toString() method from Array.prototype that returns a comma-separated string-converted version of their elements (ECMAScript Edition 3 Final, section 15.4.4.2). This method is called first on implicit string conversion (ibid., 8.6.2.6). Therefore, var myStrObj = "[" + myArray + "]"; suffices. > var options = {"val1": "test", > "val2" : myStrObj} > > I would like val2 to be an array of the values from myArray. is this > possible, or do i have to break it out manually? It is possible. Depending on your JSON-capable framework, you can use either JSON.parse() (e.g. from json2.js or json_parse.js) or simply eval() to return the respective object reference; the former is safer but probably slower than the latter. Please RTFM next time first: <http://json.org/> PointedEars -- Use any version of Microsoft Frontpage to create your site. (This won't prevent people from viewing your source, but no one will want to steal it.) -- from <http://www.vortex-webdesign.com/help/hidesource.htm>
From: SirCodesALot on 24 Jun 2008 13:07 On Jun 24, 11:41 am, Thomas 'PointedEars' Lahn <PointedE...(a)web.de> wrote: > SirCodesALot wrote: > > Is it possible to create an JSON object from a string? > > You really don't mean "JSON object" here. If there would be such a thing, > it would be an object that is referenced by the property with the name > `JSON'. Remember that JSON is a *data exchange format*, not a framework by > itself. The object that results from parsing JSON is a native user-defined > ECMAScript (Object or Array) object. > > > For example > > > var myArray = new Array(10) > > Passing a number argument in order to initialize the array is unnecessary, > as ECMAScript arrays are of variable length. It is also error-prone, as > implementations differ: in the next-to-worst case you will end up with an > array that has the number value `10' as its first element. Use > > var myArray = []; > > instead, it can be considered to be safe although not universally supported > (JavaScript 1.3+, JScript 2.0+, ECMAScript 3). > > > for (var i=0;i<10;i++) > > { > > myArray.push("{idx:"+i+",val:"+i+"}"); > > } > > Note that Array.prototype.push() requires at least JavaScript 1.2 and, more > important, JScript 5.5. You may want to augment Array.prototype or the > Array object itself with such a method. (If you choose to augment the > Array.prototype object, you should also provide an iterator method that > handles the side-effects of the augmentation.) > > <http://PointedEars.de/es-matrix/> > > Also, this only works as long as `i' is not an arbitrary string value. > > > var myStrObj = "[" + myArray.join(",") + "]"; > > Array objects inherit a toString() method from Array.prototype that returns > a comma-separated string-converted version of their elements (ECMAScript > Edition 3 Final, section 15.4.4.2). This method is called first on implicit > string conversion (ibid., 8.6.2.6). Therefore, > > var myStrObj = "[" + myArray + "]"; > > suffices. > > > var options = {"val1": "test", > > "val2" : myStrObj} > > > I would like val2 to be an array of the values from myArray. is this > > possible, or do i have to break it out manually? > > It is possible. Depending on your JSON-capable framework, you can use > either JSON.parse() (e.g. from json2.js or json_parse.js) or simply eval() > to return the respective object reference; the former is safer but probably > slower than the latter. > > Please RTFM next time first: <http://json.org/> > > PointedEars > -- > Use any version of Microsoft Frontpage to create your site. > (This won't prevent people from viewing your source, but no one > will want to steal it.) > -- from <http://www.vortex-webdesign.com/help/hidesource.htm> Thank you for both for the reply and "schooling". I know how to proceed now. BTW, I have always loved the "RTFM" reference.
From: Jeff on 24 Jun 2008 14:32
Thomas 'PointedEars' Lahn wrote: > SirCodesALot wrote: >> Is it possible to create an JSON object from a string? > > You really don't mean "JSON object" here. If there would be such a thing, > it would be an object that is referenced by the property with the name > `JSON'. Remember that JSON is a *data exchange format*, not a framework by > itself. The object that results from parsing JSON is a native user-defined > ECMAScript (Object or Array) object. > >> For example >> >> var myArray = new Array(10) > > Passing a number argument in order to initialize the array is unnecessary, > as ECMAScript arrays are of variable length. It is also error-prone, as > implementations differ: in the next-to-worst case you will end up with an > array that has the number value `10' as its first element. Use > > var myArray = []; > > instead, it can be considered to be safe although not universally supported > (JavaScript 1.3+, JScript 2.0+, ECMAScript 3). > >> for (var i=0;i<10;i++) >> { >> myArray.push("{idx:"+i+",val:"+i+"}"); >> } > > Note that Array.prototype.push() requires at least JavaScript 1.2 and, more > important, JScript 5.5. What version of IE is that? Should we care about IE versions less than 5.5? This is going on 7 years for IE6 and another year for IE5.5. I stopped adding the prototype function for push some time ago. Correct me if I'm wrong. Jeff You may want to augment Array.prototype or the > Array object itself with such a method. (If you choose to augment the > Array.prototype object, you should also provide an iterator method that > handles the side-effects of the augmentation.) > > <http://PointedEars.de/es-matrix/> > > Also, this only works as long as `i' is not an arbitrary string value. > >> var myStrObj = "[" + myArray.join(",") + "]"; > > Array objects inherit a toString() method from Array.prototype that returns > a comma-separated string-converted version of their elements (ECMAScript > Edition 3 Final, section 15.4.4.2). This method is called first on implicit > string conversion (ibid., 8.6.2.6). Therefore, > > var myStrObj = "[" + myArray + "]"; > > suffices. > >> var options = {"val1": "test", >> "val2" : myStrObj} >> >> I would like val2 to be an array of the values from myArray. is this >> possible, or do i have to break it out manually? > > It is possible. Depending on your JSON-capable framework, you can use > either JSON.parse() (e.g. from json2.js or json_parse.js) or simply eval() > to return the respective object reference; the former is safer but probably > slower than the latter. > > Please RTFM next time first: <http://json.org/> > > > PointedEars |