Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

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 08-10-2011, 02:50 PM   PM User | #1
Keltoi
Regular Coder

 
Join Date: Jun 2002
Location: Lincoln (UK)
Posts: 138
Thanks: 3
Thanked 0 Times in 0 Posts
Keltoi is an unknown quantity at this point
Import a JS array using ajax to pass though to a function/plugin

I have a generated array which if hard coded passes the array objects to a function for processing fine.

For example:
Code:
$("#termCloud").jQCloud([{text:'some',weight:10},{text:'thing',weight:8}]);
However, I need to make this more dynamic so am generating the the array externally and importing using ajax. This is what I'm Trying:

(generateArray.asp would output {text:'some',weight:10},{text:'thing',weight:8})
Code:
$.ajax({
    url: '/generateArray.asp',
    success: function(data){
        $("#wordCloud").jQCloud([data]);
    }
})
I have tried several dataTypes and all fail.

The problem seems to be that the in the working version the JQCloud plugin receives the array as objects: [object Object],[object Object] where as my ajax version receives/sends it as a string: {text:'some',weight:10},{text:'thing',weight:8}

Is there a way to import the the array and pass it though to the JQCloud function/plugin as a proper array rather than a string or convert the string to an array for processing?

It's worth noting that the return doesn't seem to be recognised as valid JSON data...

Many thanks..
__________________
asp vbscript
twitter.com/justinreid
Keltoi is offline   Reply With Quote
Old 08-10-2011, 04:17 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
Quote:
Originally Posted by Keltoi View Post
It's worth noting that the return doesn't seem to be recognised as valid JSON data...
Because it isn't ... it is a comma separated list of JSON objects. So what you need to do is: Get the string, split it at '},', add a } to each string part and JSON.parse it. Attention: This will not work if the string contains nested objects!
Code:
var jsonArray = [];
var jsonStrings = data.split('},');
for(i=0; i<jsonStrings.length; i++) {
   jsonStrings[i] += '}';
   var jsonObject = JSON.parse(jsonStrings[i]);
   jsonArray.push(jsonObject);
}
jsonArray will now be an array of JSON objects ... just as you need it.
devnull69 is offline   Reply With Quote
Old 08-10-2011, 04:34 PM   PM User | #3
Keltoi
Regular Coder

 
Join Date: Jun 2002
Location: Lincoln (UK)
Posts: 138
Thanks: 3
Thanked 0 Times in 0 Posts
Keltoi is an unknown quantity at this point
Thanks for the reply devnull69, didn't think it was JSON, indeed the original is just a string so grasping at straws...

That said, is there a specific plugin to run JSON.parse(jsonStrings[i]) as I'm getting the following error in Firebug:
Code:
JSON.parse: expected property name or '}'
__________________
asp vbscript
twitter.com/justinreid
Keltoi is offline   Reply With Quote
Old 08-10-2011, 05:32 PM   PM User | #4
Keltoi
Regular Coder

 
Join Date: Jun 2002
Location: Lincoln (UK)
Posts: 138
Thanks: 3
Thanked 0 Times in 0 Posts
Keltoi is an unknown quantity at this point
OK, just for referance I've recoded the original string so it's easier to split, I've then rebuilt the array once it's been imported... There must be an easier way but until I can experiment further (or someone can enlighten me); this seems to be working at the moment...
Original import string:
Code:
text:some,weight:10||text:thing,weight:8
Import, split and rebuild:
Code:
$.ajax({
	url: '/generateArray.asp',
	dataType: 'dataArray',
	success: function(data){
		var wArr = data.split('||'), wEle, wTxt, wWgt
		var jsonArray = [];
		for(i = 0; i < wArr.length; i++){
			wEle = wArr[i].split(',')
			wTxt = wEle[0].split(':')
			wWgt = wEle[1].split(':')
			var jsonObject = jQuery.parseJSON('{"text":"' + wTxt[1] + '","weight":' + wWgt[1] + '}');
			jsonArray.push(jsonObject)
		}
		$("#wordCloud").jQCloud(jsonArray)
	}
})
Open to any suggestions on improvement...

If not, thanks for taking a look :-)
__________________
asp vbscript
twitter.com/justinreid

Last edited by Keltoi; 08-11-2011 at 10:19 AM..
Keltoi is offline   Reply With Quote
Old 08-11-2011, 10:19 AM   PM User | #5
Keltoi
Regular Coder

 
Join Date: Jun 2002
Location: Lincoln (UK)
Posts: 138
Thanks: 3
Thanked 0 Times in 0 Posts
Keltoi is an unknown quantity at this point
Update
It seems single quotes are not good when trying to parse JSON, this works fine:
Code:
var foo = $.parseJSON('[{"text":"some","weight":10},{"text":"thing","weight":8}]');
So no need to break the string to re-build the array.. Just double quote it..

Working example:
remote script generates:
{"text":"some","weight":10},{"text":"thing","weight":8}
then the JQuery:
Code:
$.ajax({
	url: '/generateArray.asp',
	success: function(data){
		var jsonArray = $.parseJSON('['+data+']'); 
		$("#wordCloud").jQCloud(jsonArray)
	}
})
__________________
asp vbscript
twitter.com/justinreid
Keltoi is offline   Reply With Quote
Reply

Bookmarks

Tags
ajax, arrays, jquery, json

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 05:13 PM.


Advertisement
Log in to turn off these ads.