Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-27-2008, 11:03 AM   PM User | #1
harjanto
New Coder

 
Join Date: Jul 2006
Posts: 39
Thanks: 7
Thanked 0 Times in 0 Posts
harjanto is an unknown quantity at this point
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
harjanto is offline   Reply With Quote
Old 03-27-2008, 12:53 PM   PM User | #2
mic2100
Regular Coder

 
mic2100's Avatar
 
Join Date: Feb 2006
Location: Scunthorpe
Posts: 562
Thanks: 15
Thanked 28 Times in 27 Posts
mic2100 is on a distinguished road
go check this page out it will show you some examples of using JSON objects in JavaScript.

http://www.json.org/js.html
mic2100 is offline   Reply With Quote
Old 03-27-2008, 02:54 PM   PM User | #3
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
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
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 04-06-2008, 10:06 AM   PM User | #4
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,553
Thanks: 9
Thanked 480 Times in 463 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
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 5/13)
STATS (2013/5) HTML5:90.2% MOB:15.2% IE7:0.5% IE8:8.4% IE9:8.5% IE10:8.5%
rnd me is online now   Reply With Quote
Old 02-01-2012, 04:44 AM   PM User | #5
kaitana
New to the CF scene

 
Join Date: May 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
kaitana is an unknown quantity at this point
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
kaitana is offline   Reply With Quote
Old 02-01-2012, 05:07 AM   PM User | #6
kaitana
New to the CF scene

 
Join Date: May 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
kaitana is an unknown quantity at this point
Usage

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

var facUrl = "/factoryajax?json="+jsonstr;
kaitana is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 11:05 PM.


Advertisement
Log in to turn off these ads.