473,774 Members | 2,105 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

escapes and JSON

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??
Dec 4 '07 #1
4 2332
On Dec 5, 4:57 am, pbd22 <dush...@gmail. comwrote:
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.
Dec 4 '07 #2
On Dec 4, 4:48 pm, slebetman <slebet...@gmai l.comwrote:
On Dec 5, 4:57 am, pbd22 <dush...@gmail. comwrote:
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.

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.
Dec 4 '07 #3
On Dec 4, 12:57 pm, pbd22 <dush...@gmail. comwrote:
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...ascripts/fork/
json.js>

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

Peter
Dec 5 '07 #4
On Dec 5, 2:19 pm, Jeremy J Starcher <r3...@yahoo.sp am.me.not.com>
wrote:
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.)
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.
Dec 6 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
2703
by: G Matthew J | last post by:
http://htmatters.net/htm/1/2005/07/evaling-JSON.cfm This is more or less in response to Mr Crockford's admonition a few months ago, "fork if you must". Ironically, although in that usenet post he calls what I am suggesting "brittle", his own Javascript JSON parser is not valid JSON, but rather conforms to my proposed variation on JSON!! With an identifier prepended to the front of the JSON block, and function literals as values: see...
20
6883
by: Luke Matuszewski | last post by:
Welcome As suggested i looked into JSON project and was amazed but... What about cyclical data structures - anybody was faced it in some project ? Is there any satisactional recomendation... PS i am ready to use JSON as data/object interchange when using AJAX and my J2EE project - because it is easier to traverse the JavaScript object than its XML representation (so of course may argue).
2
3761
by: Kevin Newman | last post by:
Hello, I noticed that the JavaScript library for JSON posted on json.org (http://www.json.org/json.js) is modifying Object.prototype (adding a method - toJSONString). I thought this was considered bad practice because it can disrupt the use of for in loops on Objects. Am I incorrect? Thanks,
3
10179
by: Adam | last post by:
I'm trying to retrieve some values from a json object, but instead it's giving me the property name. For example: var json = { "glossary": { "title": "example glossary" } }; console.log(json); alert(json.glossary.title); for (var x in json) { console.log(x); alert(x.title); } This will show me the json object in the console with glossary and title underneath it. When the alert for json.glossary.title fires, it
2
3324
by: ChrisO | last post by:
I've been pretty infatuated with JSON for some time now since "discovering" it a while back. (It's been there all along in JavaScript, but it was just never "noticed" or used by most until recently -- or maybe I should just speak for myself.) The fact that JSON is more elegant goes without saying, yet I can't seem to find a way to use JSON the way I *really* want to use it: to create objects that can be instantated into multiple...
23
3213
by: dhtmlkitchen | last post by:
JSON We all know what it is. In ECMAScript 4, there's a JSON proposal: Object.prototype.toJSONString String.prototype.parseJSON The current proposal, String.prototype.parseJSON, returns an object.
9
10874
by: Jon Paal [MSMD] | last post by:
using json like ( {"Records": , "RecordCount":"1" } ) and jquery like: $.ajax({ .... success: function(json, status) {
6
2831
by: Lasse Reichstein Nielsen | last post by:
Max <adsl@tiscali.itwrites: Not really. It shows that a particularly naïve implementation of a conversion from XML to JSON doesn't work well. What if the conversion of <e> some
0
5381
by: crocodilu2008 | last post by:
JSON vs. XML JSON and XML are basically used for the same purpose—to represent and interchange data. I'll try to show you why you might want to use JSON rather than XML in an AJAX context by showing you an example of how an data class (actually, a list of PHP documentation pages) might be represented, first in XML. and then in JSON. This side-by-side comparison should let you begin to understand how to represent data in JSON. The XML version:...
0
9621
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10040
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9914
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8939
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6717
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3611
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2852
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.