Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Jul 2006
    Posts
    39
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Easy way to convert a Javascript array to a JSON string?

    Is there an easy way to convert a Javascript array to a JSON string equivalent of the array?

    Assume the array is comprised of strings

    Thanks for the help

  • #2
    Regular Coder mic2100's Avatar
    Join Date
    Feb 2006
    Location
    Scunthorpe
    Posts
    562
    Thanks
    15
    Thanked 28 Times in 27 Posts
    go check this page out it will show you some examples of using JSON objects in JavaScript.

    http://www.json.org/js.html

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    What do you mean by JSON string? A string is a string. Period. To convert an array to a string is enough to use the join() method
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,378
    Thanks
    11
    Thanked 592 Times in 572 Posts
    he means a string that evals into an array.

    [1,2,3,false,"hello"].toSource() works perfectly in firefox...

    use .toJSONString() in the library floating around the page referred to above for cross browser compat.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #5
    New to the CF scene
    Join Date
    May 2003
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    javascript array to JSON

    I searched a lot for this .. could not find it.

    so I would try this:

    checkIfMultiDimentional = function(arr)
    {
    for(var item in arr)
    {
    if(typeof(arr[item]) == 'object') { return true; }
    }
    return false;
    }
    myJsonify = function(thing, level)
    {
    var jsonString = "";
    if(!level) { level = 0; }
    var start;
    if(typeof(thing) == 'object') {
    if(checkIfMultiDimentional(thing))
    {
    start = 0;
    for(var item in thing)
    {
    var value = thing[item];
    if(start > 0) { jsonString += ','; }

    if(value.substring) { jsonString += item+":"+value; }
    else { jsonString += "\""+item+"\":{"+myJsonify(value, level+1)+"}"; }
    start++;
    }
    }
    else
    {
    start = 0;
    for(var item in thing)
    {
    if(start > 0){ jsonString +=','; }
    jsonString += "\""+item+"\":"+thing[item];
    start++;
    }

    return jsonString;
    }
    }
    else { jsonString = thing; }
    return jsonString;
    }

    hope this helps everyone

  • #6
    New to the CF scene
    Join Date
    May 2003
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Usage

    var jsonstr = "{"+myJsonify( multiDimentionalArray )+"}";
    //alert(jsonstr);

    var facUrl = "/factoryajax?json="+jsonstr;


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •