help with JSON

Discussion in 'Javascript' started by SirCodesALot, Jun 24, 2008.

  1. SirCodesALot

    SirCodesALot Guest

    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
     
    SirCodesALot, Jun 24, 2008
    #1
    1. Advertising

  2. On Jun 24, 9:02 am, SirCodesALot <> 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
     
    Peter Michaux, Jun 24, 2008
    #2
    1. Advertising

  3. 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>
     
    Thomas 'PointedEars' Lahn, Jun 24, 2008
    #3
  4. SirCodesALot

    SirCodesALot Guest

    On Jun 24, 11:41 am, 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.  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.
     
    SirCodesALot, Jun 24, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Florian Frank
    Replies:
    0
    Views:
    243
    Florian Frank
    Jun 30, 2009
  2. sajuptpm
    Replies:
    2
    Views:
    344
    sajuptpm
    Dec 28, 2012
  3. Acácio Centeno
    Replies:
    1
    Views:
    258
    dieter
    Feb 15, 2013
  4. Bryan Britten
    Replies:
    9
    Views:
    264
    Bryan Britten
    May 28, 2013
  5. David Karr
    Replies:
    1
    Views:
    169
    David Karr
    Jun 17, 2013
Loading...

Share This Page