By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,715 Members | 1,219 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,715 IT Pros & Developers. It's quick & easy.

How to handle hyphen/minus sign in json?

P: 62
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>

Share this Question
Share on Google+
2 Replies


JKing
Expert 100+
P: 1,206
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

P: 62
Perfect! Many thanks JKing.
Regards,
Parag Doke
May 15 '11 #3

Post your reply

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