JavaScriptSerializer(Resolved)
- hallsofvallhalla
- Site Admin
- Posts: 12031
- Joined: Wed Apr 22, 2009 11:29 pm
JavaScriptSerializer(Resolved)
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?
- hallsofvallhalla
- Site Admin
- Posts: 12031
- Joined: Wed Apr 22, 2009 11:29 pm
Re: JavaScriptSerializer
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#
javascript
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];
}
Code: Select all
var SQLDATA = '<%= javascriptarray %>';
var DialTime = SQLDATA.split(',');
Re: JavaScriptSerializer
http://msdn.microsoft.com/en-us/library/bb412179.aspx
see also:
http://stackoverflow.com/questions/4547 ... ialization
see also:
http://stackoverflow.com/questions/4547 ... ialization
Fighting for peace is declaring war on war. If you want peace be peaceful.
- Jackolantern
- Posts: 10893
- Joined: Wed Jul 01, 2009 11:00 pm
Re: JavaScriptSerializer
Oh ok! I was going to say that I believe .NET 3.5 and up can do this automatically lol.
The indelible lord of tl;dr
- hallsofvallhalla
- Site Admin
- Posts: 12031
- Joined: Wed Apr 22, 2009 11:29 pm
Re: JavaScriptSerializer
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.
- Jackolantern
- Posts: 10893
- Joined: Wed Jul 01, 2009 11:00 pm
Re: JavaScriptSerializer
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.
The indelible lord of tl;dr
- a_bertrand
- Posts: 1537
- Joined: Mon Feb 25, 2013 1:46 pm
Re: JavaScriptSerializer
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.
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.
Creator of Dot World Maker
Mad programmer and annoying composer
Mad programmer and annoying composer
- Jackolantern
- Posts: 10893
- Joined: Wed Jul 01, 2009 11:00 pm
Re: JavaScriptSerializer
Thats a good point. XML was the world's darling back when .NET first came out, and it has great, effortless support for it
The indelible lord of tl;dr
- a_bertrand
- Posts: 1537
- Joined: Mon Feb 25, 2013 1:46 pm
Re: JavaScriptSerializer
Like the XML serializer
Creator of Dot World Maker
Mad programmer and annoying composer
Mad programmer and annoying composer
- hallsofvallhalla
- Site Admin
- Posts: 12031
- Joined: Wed Apr 22, 2009 11:29 pm
Re: JavaScriptSerializer
Thanks for all the tips and suggestions.