Hi,

> ...the problem is the javascript isn't able
> to parse the JSON, though it is palpably valid (once I removed the enclosing
> quotes using jscript...

No, at that point you have invalid JSON data.  You said it comes back
as:

    "{\"count\": 0}"

If you strip off the quotes, that's:

    {\"count\": 0}

...which is not valid, the backslashes cannot appear at that position
in the data.  This is valid JSON:

    {"count": 0}

To me, it sounds like you're giving the ASP.Net serializer a string,
which it's correctly encoding for transfer (hence the quotes and
such), where the serializer is expect you to give it an *object* it
can serialize for transfer -- e.g., something with a "count" property
with the value 0, rather than a string.

> ...single quotes don't seem to work at all as
> they come through as \u0027, which is unparseable

Er, no, that's perfectly parseable.  It's a JavaScript Unicode escape
sequence for a single quote.  "\u0027" is exactly the same as "'" (or,
for that matter, "\x27").

I think you're best off pursuing this in an ASP.Net forum; the problem
is in the data being sent back rather than the Prototype JSON parser.

Good luck with it!
--
T.J. Crowder
tj / crowder software / com

On Nov 2, 11:31 am, "Nikhil Soneja" <[EMAIL PROTECTED]> wrote:
> Thanks for the quick reply TJ, much appreciated....I agree, would not want
> to fix on the client side even as a quick fix, because that will leave me
> open to browser dependent issues etc...besides would like to keep the
> jscript to a minimum, which is why I'm very grateful to have something like
> prototype!  My gut also tells me the issues are all related to how I've
> written the webservice.
>
> I did a lot of research on google though before posting my q, came across
> the same articles you mentioned (plus a lot more which sent me off on a
> tangent! eg; returning data dictionary types, which failed because those
> classes aren't serializable)...I have JSON configured as the return type on
> the webservice, and the data is coming through in that format (ie; not in
> XML tags, as it would otherwise)...the problem is the javascript isn't able
> to parse the JSON, though it is palpably valid (once I removed the enclosing
> quotes using jscript, which again I feel is because I'm returning a string
> type from the webservice)...whats strange also is that the JSON website
> lists \" as the official escape sequence for double quotes, so its strange
> that eval doesn't work with that...I do apologize also I'm probably not
> wording this that well, but have a huge deadline looming so forgive my
> incoherence! :)
>
> For reference my webservice has the following header
>     <WebMethod(EnableSession:=True)> _
>     <ScriptMethod(ResponseFormat:=ResponseFormat.JSON)> _
>     Public Function GetPageData(ByVal pageNumber As Integer, ByVal keywords
> As String) As String
>
> and internally I loop through the rows of a datatable in a dataset and write
> it as a JSON string to return.  I reduced this JSON string to a minimum for
> testing purposes, which is just the count of records as {""Count"":0} ("" is
> the escape sequence for VB.NET)...single quotes don't seem to work at all as
> they come through as \u0027, which is unparseable
>
> my prototype call in my test html file was :
> new Ajax.Request('http://localhost/ProjectSearch.asmx/GetPageData',
> {
>     method:'post',
>     contentType:'application/json;charset=utf-8',
>     postBody:'{"pageNumber":"4","keywords":"burj al arabi"}',
>     onSuccess: function(transport){
>      var outputDivObj = document.getElementById('outputDiv');
>      outputDivObj.innerHTML = transport.responseJSON.Count
>
> // tried this code to parse the json indirectly, later injected the replace
> regex in the prototype evalJSON function as // well
>       //var trimmed = transport.responseText.replace(/^\"+|\"+$/g, '') ;
>       //var jsonObj = eval(trimmed);
>       //outputDivObj.innerHTML = jsonObj.Count;
>     }
>
> });
>
> Some weird anomalies, have been debugging the prototype js with Firebug as
> well:
> responseText = "{\"count\":0}" gets parsed as responseJSON = {"count":0} (as
> a string though)
> responseText = {\"count\":0}  (with the  enclosing quotes removed) does not
> get parsed at all
> responseText = {count:0} works fine and gets parsed straight away as a
> jscript object
>
> For the time being though, I have jettisoned JSON and resorted to XML...of
> course that comes with its own issues, as I'm wading through XML Dom parsing
> now :)
>
> On Sun, Nov 2, 2008 at 2:56 PM, T.J. Crowder <[EMAIL PROTECTED]> wrote:
>
> > Hi Nikhil,
>
> > Although you could try to fix this on the client side by removing the
> > enclosing quotes and changing the escaped double quotes within to just
> > double quotes, I think you're better off dealing with the server-side
> > so it's returning properly-formatting JSON data.  That'll pay off in
> > the long run, and I'm sure ASP.Net's web services stuff can be made to
> > return valid JSON.  If not, consider ditching using the web services
> > stuff and just use ASP.Net directly.
>
> > But if you're going to deal with it on the client-side, you can
> > probably do it with a regex (or a series of them) using JavaScript's
> > String.replace or Prototype's String.gsub.  Basically, look at the
> > string escaping rules in the spec[1] and apply them in reverse.  I
> > would suggest *not* being tempted by the easy option of just replacing
> > \" with "; you'll need to handle all of the single-character escapes
> > (' " \ b f n r t v, I think, but check) as well as hex escapes and
> > unicode escapes.
>
> > But again, almost certainly best to handle this by fixing what the
> > server's returning.  I searched for "+asp.net +"web service" +json"
> > and found quite a few articles, including this one[2] that says "The
> > default serialization of AJAX-based Web services is JSON." and "...one
> > of the major additions of the ASP.NET 2.0 AJAX Extensions is a JSON
> > serializer."
>
> > [1]http://www.ecma-international.org/publications/standards/Ecma-262.htm
> > [2]http://msdn.microsoft.com/en-us/magazine/cc163499.aspx
>
> > FWIW,
> > --
> > T.J. Crowder
> > tj / crowder software / com
>
> > On Nov 2, 9:19 am, Nikhil <[EMAIL PROTECTED]> wrote:
> > > Hi,  Have just started using prototype with ASP.NET 2.0 AJAX
> > > Webservices and JSON two days ago, and have run into a very strange
> > > roadblock, ok let me explain the basic details of my app first :  My
> > > ASP.NET webservice has one function that returns a dataset in JSON
> > > format (I've manually transformed the dataset into JSON looping
> > > through the rows)...the webservice returns the value as a string
> > > (which was the only way I could get it to work)...now when I consume
> > > the webservice using AJAX.request, the transport.responseJSON remains
> > > a string not an object!  What seems to happen is that there's an extra
> > > set of double quotes wrapped around the string returned by the
> > > webservice...I have the feeling this is because of the webservice
> > > returning a string value.  Ok, now as a test, I modified the
> > > javascript evalJSON function to trim the quotes around the string
> > > before parsing it using eval...this still doesn't work if any of the
> > > parameters inside the JSON formatted string are enclosed in double
> > > quotes either, the reason being these get interpreted as \" which is
> > > invalid, essentially VB.NET: {""Count"":10} get translated to in
> > > transport.responseText as {\"Count\":10}...however as per JSON specs,
> > > parameter names and string values must be enclosed in double quotes,
> > > so can't do away with it!  If anyone could help out with this two-fold
> > > problem ie; double quotes around the returned webservice string
> > > result; and the \" issue, I would be eternally grateful!  If anyone
> > > has any code samples that would be super as well thanks!  Regards,
> > > Nikhil
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to