PDA

View Full Version : Problem with JSON.decode



TheBigOnion
25 May 2010, 7:39 AM
Hello all,
how do I make my json safe for decoding? Take a look:

{body: "/n/rThis is my body",subject: "This is my subject"}

fails when I put it into my Ext.util.JSON.decode

it does not like the /n/r but I do know know how to make it safe for the decoder.

Can someone help me learn how to set up my json data so all unsafe characters are safe?

Thanks,
Michael

wm003
25 May 2010, 8:04 AM
The given String is no valid JSON.
How was the string encoded? A Json-encoder will automatically mask the slashes out to

{"body":"\/n\/rThis is my body","subject":"This is my subject"}

Example for PHP


<?php
echo json_encode(array(
"body" => "/n/rThis is my body",
"subject" => "This is my subject"
));
?>

steffenk
25 May 2010, 8:08 AM
/n/r is wrong anyway. linebreak is "\n" (unix) For html use <br> or <br />

TheBigOnion
25 May 2010, 9:26 AM
Sorry, I typed it in wrong on this thread. It is "\n\r".

The data is being read from a file and I don't actually see the line breaks, but they are there. If I view the result from Ext.Ajax.request....result.responseText, I can see that the results come on serveral lines because of line breaks, but visually I don't actually see the "\n\r".

I hope that makes since.

steffenk
25 May 2010, 9:44 AM
what's with the tip of wm003? It's exactly what you need. or different language?

TheBigOnion
25 May 2010, 10:01 AM
what's with the tip of wm003? It's exactly what you need. or different language?


I am not sure what he is talking about. I don't need a different language, I am using Javascript. That's what ExtJS is.

All I know is that I am trying to retrieve some data via the Ext.Ajax. The data comes back fine, but when I try to run
the Ext.util.JSON.decode(myData) it errors out in my try...catch. If I try to spit out my err.description it just says "undefined".

If I use FireBug to look at the return of my AJAX, I can see the JSON data with many line breaks in it as you would expect in a paragraph. (I'm talking about the value of one of my name/value pairs) I can just see the line breaks not the actual "\n\r". So I'm not really sure what the ascii characters are. Anyhow, when it has the line breaks in it my decode fails.

I really do appreciate any help on this.

steffenk
25 May 2010, 10:04 AM
you retrieve json data from where? You do what? Give us some code (copy json data, your decode code).

TheBigOnion
25 May 2010, 10:13 AM
you retrieve json data from where? You do what? Give us some code (copy json data, your decode code).

It does not really matter where I get the json data from. Here is some code:
"DOES NOT WORK"
var jsonData = Ext.util.JSON.decode("{body:\"my new \n text\", subject: \my new subject\"}");

"WORKS"
var jsonData = Ext.util.JSON.decode("{body:\"my new text\", subject: \my new subject\"}");


Again, this is the only way I can show it to you. If I were to view the one that does NOT work in my FireBug it would look like this

{body:\"my new
text\", subject: \my new subject\"}


I hope this helps.

mrusinak
25 May 2010, 10:20 AM
If this is exactly what your JSON looks like:

{body: "\n\rThis is my body",subject: "This is my subject"}Then the problem isn't the control characters, it is that the member variables aren't enclosed in double quotes. It should be:

{"body": "\n\rThis is my body","subject": "This is my subject"}Now if your JSON actually has the newline or carriage return, and not their control character escapes, then I'm sure that is also invalid JSON (or at the very least, "not a good idea") - which means whatever is generating the JSON is not doing so correctly.

Remember, that JSON is string that describes javascript - but in the end, it's literally a string. http://www.json.org/ is written out very well in my opinion, and should be pretty clear on how a JSON string is supposed to look.

Edit - Saw your post above. If your string actually contains a newline, and not it's control character, then yes I would expect that to fail to decode.

TheBigOnion
25 May 2010, 10:27 AM
I changed it to:


var jsonData = Ext.util.JSON.decode("{\"body\":\"my new \n text\", \"subject\": \my new subject\"}");
but I receive the same error.

wm003
25 May 2010, 9:27 PM
That's also invalid json either :S
- You forgot a quote between \ and m.
- you need to mask the \ from \n to \\n

Again: How do you _encode_ your data into JSON? Are you doing it manually? Seems so.
Ext.Ajax retrieves Data from a server which usually generates the Response-JSON-Data by some cgi script in jsp/php/asp/whatever

Anyway, try this (using single quotes for the whole JSON-String will make reading/understanding easier):


var jsonData = Ext.util.JSON.decode('{"body":"my new \\n text ", "subject": "my new subject"}');

TheBigOnion
27 May 2010, 8:04 AM
Yes, that gets rid of the error, but then I no longer have line breaks in my paragraph. I need to keep the line breaks the user types into their paragraph, but when I read their file off the server and pass it back as JSON to my page, I need the Ext.JSON to be able to read it.

evant
27 May 2010, 8:35 AM
You can't. You need to replace any literal newlines with \r\n or whatever is appropriate.

mrusinak
27 May 2010, 8:39 AM
Yes, that gets rid of the error, but then I no longer have line breaks in my paragraph. I need to keep the line breaks the user types into their paragraph, but when I read their file off the server and pass it back as JSON to my page, I need the Ext.JSON to be able to read it.

When you say you no longer have line breaks, do you mean that displaying the paragraph on the webpage no longer has linebreaks? That will depend on how you are rendering the paragraph to the webpage - if its into a <p> or a <div> or something, you might need to convert those \n's to <br/>'s. If you're rendering the paragraph into a textfield or textarea, then I believe \n is the correct linebreak to use, so that would point to being encoded incorrectly. I think :)

TheBigOnion
27 May 2010, 11:23 AM
Thank you mrusinak, it is now solved.