aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes How to use JSON & eval() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "How to use JSON & eval()" Watch "How to use JSON & eval()" New topic
Author

How to use JSON & eval()

Allen Williams
Ranch Hand

Joined: Sep 04, 2006
Posts: 136
I'm making an attempt here to use a JSON interface in a sort-of (or soon-to-be) AJAX-type paradigm. The Java servlet returns the following:


touser=response{"result":"OK"};


I have this in the Javascript:

This is where I get:

missing ; before statement

I've tried various combinations and permutations of syntax, sometimes getting the object touser just being an array of single characters, but never getting it as the object that I think it's obvious I'm trying to get.

Really, I don't even want "touser". I'd really just like to pass something like

response{"result":"OK"};

in other words, a response object that, in the called program, the user could name whatever he wanted to access it.

BTW, I copied this directly from (several) web sites that claimed this was the way to do it, so I don't know what I'm missing.

I'm using Mozilla Firefox or Iceweasel, depending on the OS I'm on at the moment.

TIA,


-------<br />Thanks & regards,<br />anw
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61218
    
  66

Originally posted by Allen Williams:
The Java servlet returns the following:

touser=response{"result":"OK"};


What's with the extra goo?

Just have the servlet return the JSON construct, and then do something like:


[ April 19, 2007: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Allen Williams
Ranch Hand

Joined: Sep 04, 2006
Posts: 136
I've tried this:


and, after reading Bear's post, I tried this:

In both cases, I get this:

Server returned 'response{"result":"OK"};'
(also, note, I've tried sending back:
Server returned 'response{result:"OK"};'
i. e., no quotes on result)

At the showObject call (Print 2 in the first listing, which is a little object printer) I get

Object srvResponse has the following name/value pairs:
name='0' value='r'
name='1' value='e'
name='2' value='s'
...and so on for the whole return string.

I have tried Bear's code (I think as well as all kinds of other syntactical variation I've found on the web and on a couple of other forums, but can't seem to get this return recognized as an JS object.

BTW, is there some convenient way to have e*v*a*l as the real word in my post?
TIA,
Allen
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61218
    
  66

Sorry, but since I have no idea what showObject does, I can't really interpret your results.

Try paring your example down to a snippet that just shows your issue.

Perhaps something along the lines of:



to get the hang of using eval().

BTW, is there some convenient way to have e*v*a*l as the real word in my post?


Yes: eval() eval() eval() eval() eval() eval() eval() eval()

Hint: replace any character in eval() with its HTML entity equivalent.
[ April 21, 2007: Message edited by: Bear Bibeault ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61218
    
  66

Here's a little example I whipped up:



Whether responseText is hard-coded, as it is here, or comes from an Ajax request is moot.
[ April 21, 2007: Message edited by: Bear Bibeault ]
Allen Williams
Ranch Hand

Joined: Sep 04, 2006
Posts: 136
OK, thanks for the hints. I'll try it and get back in a few. The showObject doesn't do anything except put up an alert with the name value pairs. Thanks also for the eval hint. Back in a few...
Allen Williams
Ranch Hand

Joined: Sep 04, 2006
Posts: 136
OK, I did as you suggested and just made a bare-bones "play with the variables" code block. I got rid of showObject, although it did nothing but print, and just used Firebug's console print (think I like it better anyway).

Here's the code:

and here's what printed on the console:

server returned 'response{result:"OK"};'
tstvar= 'response{result:"OK"};'; resp.result= undefined
tstvar= 'response{"result":"OK"};'; resp.result= undefined
tstvar= 'resp=response{"result":"OK"};'; resp.result= undefined
tstvar= '{"result":"OK"};'; resp.result= undefined

I'm sort of running out of syntactical variations here.

Thanks SO much
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61218
    
  66

Naturally, it's going to fail unless you use valid JSON.

server returned 'response{result:"OK"};'


is not.
[ April 21, 2007: Message edited by: Bear Bibeault ]
Allen Williams
Ranch Hand

Joined: Sep 04, 2006
Posts: 136
Actually, just looking at it now, I can tell the first two and probably three are syntactically invalid. I don't think "resp=response{result:"OK"}" is valid in or out of an eval, or you'd have to access it some weird way like resp.response.result. Nonsense there.

What's wrong with the last one, though? Isn't it just like your example, except "OK" is a string?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61218
    
  66

What's with all the weird quoting?



should be



It could be even further simplified by removing the uncessary quotes around result.
[ April 21, 2007: Message edited by: Bear Bibeault ]
Allen Williams
Ranch Hand

Joined: Sep 04, 2006
Posts: 136
Yeah, you're right. I did have "result:\"OK\"", so had to escape them. I then went half way to getting rid of them. I'll see right now if that's screwing it up.
Allen Williams
Ranch Hand

Joined: Sep 04, 2006
Posts: 136


tstvar= {result:"OK"};; resp.result= OK


Well, that did it! Now, let me see if I can return it from the server like that.

Thanks SO much for the help!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to use JSON & eval()