escapes and JSON

P

pbd22

hi, this has been a bugger for me.

JSON:

{" + escape('some (annoying) text') + " : "friendly text" }

i want to escape the text inside the escape function but
show the results inside double quotes!

The above line puts everything inside the double quotes and
the escape function is now text.

How is this done??
 
S

slebetman

hi, this has been a bugger for me.

JSON:

{" + escape('some (annoying) text') + " : "friendly text" }

You're quoting the function there.

If you LITERALLY want the double quotes:

{'"' + escape('some (annoying) text') + '"' : "friendly text" }

But this is strange since the "friendly text" is not in literal double
quotes. If instead you want the result of the escape function to have
the same quoting level as the friendly text:

{ escape('some (annoying) text') : "friendly text" }

But it's rarely (never?) a good idea to have special characters in
your key/attribute name.
 
P

pbd22

You're quoting the function there.

If you LITERALLY want the double quotes:

{'"' + escape('some (annoying) text') + '"' : "friendly text" }

But this is strange since the "friendly text" is not in literal double
quotes. If instead you want the result of the escape function to have
the same quoting level as the friendly text:

{ escape('some (annoying) text') : "friendly text" }

But it's rarely (never?) a good idea to have special characters in
your key/attribute name.


OK, thanks.

SO, how do handle special case characters in my JSON strings?

I have keys such as "The Last Mermain (1994) - Limited Edition".

I agree with you, escape really messes things up. But, the
special characters are causing all sorts of probs with JSON
syntax.

Please help... deadline.

THanks.
 
J

Jeremy J Starcher

On Tue, 04 Dec 2007 14:45:17 -0800, pbd22 wrote:

[snip of some sample code]
OK, thanks.

SO, how do handle special case characters in my JSON strings?

I have keys such as "The Last Mermain (1994) - Limited Edition".

I agree with you, escape really messes things up. But, the
special characters are causing all sorts of probs with JSON
syntax.

Please help... deadline.

THanks.

Chances are, with that key like that, you have a design issue. I can't
see your code, so I'm not 100% certain -- but I strongly suggest you
look things over and go "Why?"

That said, if you are totally sure you want to do this, there might
be a better way to get what you are after. (Even with the best CRC
method there is still a (very small) chance collisions. Reading up
on hash tables and how to handle collisions isn't a bad idea.)

Checksums, like a CRC checksum.


http://www.digsys.se/js_crc.html

var key = "'some (annoying) text'";
var keyhash = crc(key);
alert("Keyhash = " + keyhash); // 0x4DE4C95E

var MyObj = {}
MyObj[keyhash] = "friendly text";

If anything of value is lost by hashing the key -- if in the running of
your program changing 'some (annoying) text' to 0x4DE4C95E matters --
then you have a bad key/value design.


good:
var book = {"title": "The Last Mermain (1994) - Limited Edition",
"pages" : 8008};


Bad:
var badbook = {"How to Win Friends and Influence People": "book",
{"details" : "80 pages."}
};
 
P

Peter Michaux

hi, this has been a bugger for me.

JSON:

{" + escape('some (annoying) text') + " : "friendly text" }

i want to escape the text inside the escape function but
show the results inside double quotes!

The above line puts everything inside the double quotes and
the escape function is now text.

How is this done??

Have you thought about using a JSON dumper so you don't have to worry
about this stuff?

var obj = {'some (annoying) text':'friendly text'};

FORK.Json.dump(obj)

<URL:http://dev.michaux.ca/svn/fork/trunk/public/javascripts/fork/
json.js>

<URL:http://forkjavascript.org/json/docs>

Peter
 
S

slebetman

On Tue, 04 Dec 2007 14:45:17 -0800, pbd22 wrote:

[snip of some sample code]


OK, thanks.
SO, how do handle special case characters in my JSON strings?
I have keys such as "The Last Mermain (1994) - Limited Edition".
I agree with you, escape really messes things up. But, the
special characters are causing all sorts of probs with JSON
syntax.
Please help... deadline.

Chances are, with that key like that, you have a design issue. I can't
see your code, so I'm not 100% certain -- but I strongly suggest you
look things over and go "Why?"

That said, if you are totally sure you want to do this, there might
be a better way to get what you are after. (Even with the best CRC
method there is still a (very small) chance collisions. Reading up
on hash tables and how to handle collisions isn't a bad idea.)

Well, one way of totally avoiding collisions is to simply base64
encode the key. If base64's '+' and '/' characters still give you
trouble then simply convert the key to hex which should handle
anything you care to throw at it. But if you do go the hex route
remember to be consistent and either ALWAYS use uppercase OR ALWAYS
use lowercase since keys are case sensitive.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top