Results 1 to 8 of 8

Thread: [SOLVED] JSON response with keys that contain "-" hyphen character ref error

  1. #1
    Sencha User cavalleydude's Avatar
    Join Date
    Apr 2010
    Posts
    57
    Vote Rating
    0
      0  

    Default [SOLVED] JSON response with keys that contain "-" hyphen character ref error

    I am receiving a JSON response that contains "-" hyphens in some keys, it decodes okay, but when I attempt to reference any values in the JSON object, I get errors. See example below.

    var jsonObj = Ext.util.JSON.decode( response.responseText );

    // the jsonObj contains a key md-number
    // try to reference the field/value

    var mdno = jsonObj.md-number; // throws an error during runtime 'undefined number'

    So, my question is how do I reference these keys that contain a hyphen?

    PS: I've looked at the jsonObj in Firebug and the object contains all the key/values pairs. So it was decoded without error. I just cannot access keys with "-" in it.

    Any help would be appreciated.

  2. #2
    Sencha User
    Join Date
    Jun 2009
    Location
    Colorado
    Posts
    51
    Vote Rating
    0
      0  

    Default

    Just a guess, but try jsonObj['md-number']

  3. #3
    Sencha User
    Join Date
    Sep 2009
    Location
    Calgary, Alberta, Canada
    Posts
    638
    Vote Rating
    2
      0  

    Default

    @Keylan: That's correct.

    the - is an operator, and will treat it that way. So to access the property, you will need to do what Keylan has suggested.

  4. #4
    Sencha User cavalleydude's Avatar
    Join Date
    Apr 2010
    Posts
    57
    Vote Rating
    0
      0  

    Default

    YES! That did it! Perfect!

    Thank you!

  5. #5
    Sencha User
    Join Date
    Sep 2009
    Location
    Calgary, Alberta, Canada
    Posts
    638
    Vote Rating
    2
      0  

    Default

    Is it [SOLVED]?

  6. #6
    Sencha User cavalleydude's Avatar
    Join Date
    Apr 2010
    Posts
    57
    Vote Rating
    0
      0  

    Default

    Yes, it is solved.

  7. #7
    Sencha User
    Join Date
    Sep 2009
    Location
    Calgary, Alberta, Canada
    Posts
    638
    Vote Rating
    2
      0  

    Default

    Then I would suggest you edit your original post, and append a prefix of [SOLVED]. Much appreciated.

  8. #8
    Sencha Premium Member
    Join Date
    Nov 2012
    Location
    Bangalore
    Posts
    79
    Vote Rating
    1
      0  

    Default

    I was facing other problem on how to specify the hyphen in my store root object
    My nested data looks like

    Code:
    {
        "data": {
            "device-config": {
                "device": {
                    "mac": "00-11-22-33-A8-CA",
                    "hostname": "Ap228382",
                    "timezone": "Etc/UTC",
                    "country_code": "ai"
                }
            }
        }
    }


    And my store will read the nested data with hyphen as below
    Code:
    root: 'data["device-config"].device'

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •