Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > help with JSON

Reply
Thread Tools

help with JSON

 
 
SirCodesALot
Guest
Posts: n/a
 
      06-24-2008
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
 
Reply With Quote
 
 
 
 
Peter Michaux
Guest
Posts: n/a
 
      06-24-2008
On Jun 24, 9:02 am, SirCodesALot <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      06-24-2008
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>
 
Reply With Quote
 
SirCodesALot
Guest
Posts: n/a
 
      06-24-2008
On Jun 24, 11:41*am, Thomas 'PointedEars' Lahn <(E-Mail Removed)>
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Lib to generate XML/JSON[P] output from a DTD/XSD/JSON Schema/etc Accio Centeno Python 1 02-15-2013 07:34 AM
I am facing an issue while decoding json string using json.loads sajuptpm Python 2 12-28-2012 07:16 AM
[ANN] Security Fix json-1.1.7 for json_pure and json gems Florian Frank Ruby 0 06-30-2009 05:18 PM
"JSON for ASP" at json.org Turul Topuz ASP General 1 06-27-2008 11:37 PM
json gem error help please? Chris Scott Ruby 1 04-04-2008 07:28 PM



Advertisments