Don’t use string concatenation for JSON string, use JSON.stringify()

As javascript newbie, this is what I did.


return '{"Data" : '
		+ '{'
			+ '"uid" : "' + this.uid + '", '
			+ '"approval" : "' + ($('.date-picker', this.form).val() || '') + '", '
			+ '"count" : ' + this.uploader.getcount() + ', '
			+ '"size" : ' + this.getsize() + ', '
			+ '"fid" : ' + vm.getfid() + ', '
			+ '"Notifees" : [], '
			+ '"Assignees" : [], '
			+ '"AssigneesPersonalMessage" : "", '
			+ '"NotificationPersonalMessage" : "", '
			+ '"TeamId" : 0, '
			+ '"Addresses" : ""'
		+ '}'
	+ '}';

But don’t do this. You can use JSON.stringify(). it converts an object to JSON notation representing it.

With that, the code changes to this.

var metaData =                 {
		"Data" : {
			"uid" : self.uid, 
			"count" : 1, 
			"size" : self.getsize(),
			"fid" : vm.getfid(),
			"approval" : "",
			"Notifees" : [],
			"Assignees" : [],
			"AssigneesPersonalMessage" : "",
			"NotificationPersonalMessage" : "",
			"TeamId" : 0,
			"Addresses" : ""
		}
	};

return JSON.stringify(metaData);

Don’t use string concatenation for JSON string, use JSON.stringify()

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s