Page 1 of 1
JavaScriptSerializer(Resolved)
Posted: Thu Jun 13, 2013 8:09 pm
by hallsofvallhalla
So I am wroting a asp.net site for work and I need to convert a C# array to a javascript array as I am using C# to query the DB but using Google Charts. Using the JavaScriptSerializer class is a little silly and whom ever wrote it should be kicked in the jimmy. Anyone know a better way?
Re: JavaScriptSerializer
Posted: Thu Jun 13, 2013 8:35 pm
by hallsofvallhalla
ah ha! I am using .net 2.0 due to limitations on server so that is why i am having issues
I went with just turning it into a string then splitting it out
c#
Code: Select all
string javascriptArray = "";
for (int i = 0; i < DialTime.Length; i++)
{
javascriptArray = javascriptArray + "," + DialTime[i];
}
javascript
Code: Select all
var SQLDATA = '<%= javascriptarray %>';
var DialTime = SQLDATA.split(',');
Re: JavaScriptSerializer
Posted: Thu Jun 13, 2013 9:30 pm
by Chris
Re: JavaScriptSerializer
Posted: Thu Jun 13, 2013 10:31 pm
by Jackolantern
Oh ok! I was going to say that I believe .NET 3.5 and up can do this automatically lol.
Re: JavaScriptSerializer
Posted: Fri Jun 14, 2013 1:49 am
by hallsofvallhalla
Y eah i am on 2.0 and building this site from scratch. Uploading 150k entries a day then running reports on all the data. fun stuff.
Re: JavaScriptSerializer
Posted: Fri Jun 14, 2013 1:58 am
by Jackolantern
I still love .NET. It is a great platform, even if MS sometimes puts silly roadblocks in the way that just seem to make some things harder (setup is often one of those). Even though node is going splendidly for me and I am really liking it, I still want to check out SignalR sometime.
Re: JavaScriptSerializer
Posted: Fri Jun 14, 2013 4:45 am
by a_bertrand
First of all, if you are building a long string use StringBuilder not the string class. That would be MUCH faster.
To come back to the question, all depends how complex is your object tree. If it's just like a single array, you could very well do the JSON by hand. I do remember there was also a try involving web services which was producing JSON strings. I will check back if I have some code left using it.
Finally, why not try instead to produce an XML file (where C# can generate it since nearly .NET 1)? As JS can decode it without too much efforts.
Re: JavaScriptSerializer
Posted: Fri Jun 14, 2013 7:53 am
by Jackolantern
Thats a good point. XML was the world's darling back when .NET first came out, and it has great, effortless support for it

Re: JavaScriptSerializer
Posted: Fri Jun 14, 2013 8:50 am
by a_bertrand
Like the XML serializer

Re: JavaScriptSerializer
Posted: Fri Jun 14, 2013 1:43 pm
by hallsofvallhalla
Thanks for all the tips and suggestions.