473,543 Members | 2,434 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to handle hyphen/minus sign in json?

62 New Member
Hello All.
I am new to json and need to work with some json output generated by code which is outside my control. I was trying out some simple alert examples.

Noticed that the hyphen or minus sign runs into problems. While this seems JavaScript wise logical (variable names cannot have operators), I don't know how to handle them.

Here is my made up example that does not work:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script language="JavaScript">
  4. var o={
  5.   "parent-object":   {
  6.     "child-object01":     {
  7.       "name": "Anne"
  8.     },
  9.     "child-object02":     {
  10.       "name": "John"
  11.     },
  12.     "child-object03":     {
  13.       "name": "Julia"
  14.     },
  15.     "name": "Andrew"
  16.   }
  17. };
  18.  
  19. </script>
  20. </head>
  21. <body>
  22.     <a onclick="javascript:alert(o.parent-object.child-object01.name);">Click me</a>
  23. </body>
  24.  
If I replace all hyphens with underscores, then the alert does display "Anne".

How does one handle such cases when the json input cannot be changed ?
I tried searching on bytes for an answer, but the closest match I found was for double quotes. Here is the link to it:
http://bytes.com/topic/javascript/answers/911058-json-object-escape-sequence-double-quotes-problem
May 14 '11 #1
2 16587
JKing
1,206 Recognized Expert Top Contributor
Hello,

If you access the JSON object like it is an associative array you can work around this.
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script language="JavaScript">
  4. var o={
  5.   "parent-object":   {
  6.     "child-object01":     {
  7.       "name": "Anne"
  8.     },
  9.     "child-object02":     {
  10.       "name": "John"
  11.     },
  12.     "child-object03":     {
  13.       "name": "Julia"
  14.     },
  15.     "name": "Andrew"
  16.   }
  17. };
  18.  
  19. </script>
  20. </head>
  21. <body>
  22.     <a onclick="javascript:alert(o['parent-object']['child-object01']['name']);">Click me</a>
  23. </body>
May 15 '11 #2
paragpdoke
62 New Member
Perfect! Many thanks JKing.
Regards,
Parag Doke
May 15 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
4332
by: mm | last post by:
In a regular expression, is a hyphen simply - whereas a minus sign is \- I am trying to understand the code below: $addend_two = (rand 200) - 100; $addend_two =~ /(\-?+)\.?*/ ? $intPart2 = $1 : die "No integral value\n" ;
27
31367
by: The Bicycling Guitarist | last post by:
Hi. I found the following when trying to learn if there is such a thing as a non-breaking hyphen. Apparently Unicode has a ‑ but that is not well-supported, especially in older browsers. Somebody somewhere said: Alternately, you can use CSS to declare a class having: ..nowrap { white-space:nowrap } .... and then wrap the compound word...
2
3203
by: Max | last post by:
I'm reading some files in a directory using php code for the end result of allowing these files to be deleted. The problem is some of the file names have hyphens in them, and this cannot be changed. The php code prints each file name on a page and has a delete link for each seperate file. I pass the name of the file via a variable using...
13
49843
by: Matt | last post by:
I would like to set the "list-style-type" to be a hyphen (-). How can I accomplish this in a style sheet. I tried list-style-type: hyphen; and list-style-type: dash; but neither worked. I also tried adding this to the style sheet (with the list-style-type set to none in the UL element). LI:Before { content: "- "; }
22
7978
by: stevenkobes | last post by:
If a word has a hyphen in it, IE will permit a line break at the hyphen, but Firefox/Mozilla won't. Apparently the Firefox behavior is standards-compliant, but it is not what I want. Is there a way to denote a hyphen in HTML, that the line can be broken after? I've read some stuff about soft hyphens and non-breaking hyphens, but those...
4
4537
by: Rubin | last post by:
1) I want to show a breaking hyphen in Mozilla 1.5.0.4 How do I do that? "Unicode standard annex #14", <http://www.unicode.org/reports/tr14/>, defines 4 breaking hyphens. <quote> Breaking hyphens establish explicit break opportunities immediately after each occurrence.
1
3412
by: jmdolinger | last post by:
Hi all, Has anyone ever seen the following Javascript problem (in IE). I'm trying to set up a hash of rating values to integers (which I'll then use to sort an array of ratings). It looks like this: var ratingsTable = new Object(); ratingsTable = 0; ratingsTable = 1; ratingsTable = 2;
2
19321
by: Chris | last post by:
I can't figure out how to handle multidimensional arrays with json created with ajax/php e.g. {"452":{"name":"chris","age":"21"},"798":{"name":"sam","qty":"22"}} I use the following code for simple arrays but don't know how to handle multidimensional arrays? json_object = eval( "(" + xmlHttp.responseText + ")" );...
3
1449
by: Tim Kuzniar | last post by:
In a SQL statement of a query there is a line that reads: + 1 I have turned it into a parameter query by rewriting it as: + which, of course, allows me to pick a number to add on the fly when I run the query.
0
7347
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...
0
7590
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7344
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...
0
5883
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...
1
5264
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3388
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1814
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
963
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
633
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...

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.