Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to handle newlines in JSON 1

Status
Not open for further replies.

OsakaWebbie

Programmer
Feb 11, 2003
628
JP
I am using PHP on the server-side to pass database data back to Javascript via JSON, and sometimes I'll need to pass the same data back and forth a couple times (while various user feedback is requested to finalize it), but finally, it will go in the database as regular data and on the page as "innerHTML" content. But I'm having problems with newlines - of course if I put one in the JSON string as is, the eval() gives an "unterminated string literal" error, but the strange thing is that even if I change the newlines to "<br />" when constructing the JSON, eval() still gives the same error. Does Javascript really interpret "<br />" as a newline internally? If so, what should I use to represent newlines? I haven't even yet worried about how to get it back to the server-side once it's been transformed to something that Javascript likes - I figured I'd cross that bridge when I come to it - but if someone has been down this road and has a method for making a round-trip with the data, that's a bonus.

(In case any of you notice this same question on the Javascript forum... I did not initially double-post, but was "sent" here after posting there.)
 
eval will never convert the literal string "<br />" into the escape sequence "\n". It just won't happen.

Chances are you have other whitespace being output by your server. The best bet to find this is to visit the URL your AJAX is requesting, and see for yourself if your content (before EVAL) has any CR/LF chars in it.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Thanks for the clue. Indeed, after some more digging I figured out that I had shot myself in the foot with what I thought was a handy function - for years I used my own routine to prepare database text for use in HTML by using regexp to replace any combination of \n and \r to a BR tag, but more recently I discovered the built-in function nl2br() and started using that instead. Oops! It does put the BR in, but it doesn't take out the newline character(s). For HTML that's harmless, but for JSON...

When I got that aspect working, I expected to have to then add more code for the return trip (client-to-server) to make the replacement the other direction so that I didn't have a bunch of BR tags in my database data. But interestingly, somehow it seems to just work itself out - after a round trip the database appears to contain a regular newline. (My multibyte characters all changed into question marks, but that's a different matter...)

Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top