1

I want to return an array of javascript objects from my asp.net asmx file. ie.

variable = [
    {
        *value1*: 'value1',
        *value2*: 'value2',
        ...,
    },
    {
    .
    .
    }
];

I seem have been having trouble reaching this. I'd put this into code but I've been hacking away at it so much it'd probably do more harm than good in having this answered.

Basically I am using a web service to find names as people type the name. I'd use a regular text file or something but its a huge database that's always changing - and don't worry I've indexed the names so searching can be a little snappier - but I would really prefer to stick with this method and just figure out how to get usable JSON back to javascript. I've seen a few that sort of attempt to describe how one would approach this but I honestly think microsofts articles are damn near unreadable.

Thanks in advance for assistance.

EDIT: I'm using the $.ajax() function from jQuery - I've had it working but it seems like I was doing it in bad practice not returning and using actual JSON. Previously I'd take a string back and insert it into html to use the variable it set - very roundabout.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Jon Phenow
  • 3,974
  • 5
  • 26
  • 30
  • http://stackoverflow.com/questions/1056169/serialize-to-json-in-net-2-0 Has some more info. I've used the JavaScriptSerializer class. – pritaeas Jun 15 '10 at 13:16
  • If you replace ASMX with WCF, it gets a lot easier. – Steven Sudit Jun 15 '10 at 13:18
  • How so? Whats the big difference? – Jon Phenow Jun 15 '10 at 15:27
  • big difference number 1: Microsoft now considers ASMX to be "legacy technology" and recommends all new development use WCF. big difference number 2: WCF has about 10 times the features of ASMX, to be conservative. – John Saunders Jun 15 '10 at 18:08
  • How much difficulty is it to switch over and still use it with jquery and so forth? I just spent half the work day converting my whole site to C# because I was stupid for even thinking VB was ever a good idea in any sense of the concept. – Jon Phenow Jun 15 '10 at 21:17

1 Answers1

0

I just figured I'd answer because I hate when people ask a question, discover the answer, then don't share.

I went here http://weblogs.asp.net/scottgu/archive/2007/10/01/tip-trick-building-a-tojson-extension-method-using-net-3-5.aspx which convinced me to convert my site from vb to C# because I actually understand why C# works and I hated vb from the start - I just figured it'd maybe get better and be worth learning a little of. literally all I did was implement that tiny little static class and bam it works how I like it.

Jon Phenow
  • 3,974
  • 5
  • 26
  • 30
  • Thanks, similar to what I did here: http://stackoverflow.com/questions/2225781/asp-net-web-method-that-accepts-a-listcustomobject-is-failing-with-web-service/2268178#2268178 – Mark Schultheiss Jun 15 '10 at 18:24