473,471 Members | 1,858 Online
Bytes | Software Development & Data Engineering Community
Create 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

✓ answered by JKing

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>

2 16537
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
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 =...
27
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...
2
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...
13
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...
22
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...
4
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...
1
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...
2
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...
3
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...
0
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,...
0
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...
0
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,...
0
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...
0
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,...
1
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.