0

Possible Duplicate:
How do I reference a javascript object property with a hyphen in it?

I have a Json of below format :

var response1="{" +
                        "\"code\":\"200\"," +
                        "\"requestID\":\"1002\"," +
                        "\"body\":\"[{" +
                        "\\\"author\\\":\\\"sumit\\\"," +
                        "\\\"id\\\":\\\"ABX-002\\\"," +
                        "\\\"title\\\":\\\"How to make Android APK in 2 seconds :)\\\"" +
                        "}," +
                        "" +
                        "{" +
                        "\\\"author\\\":\\\"sumit\\\"," +
                        "\\\"id\\\":\\\"ABX-002\\\"," +
                        "\\\"title\\\":\\\"How to make Android APK in 2 seconds :)\\\"" +
                        "}," +
                        "{"+
                        "\\\"author\\\":\\\"sumit\\\"," +
                        "\\\"id\\\":\\\"ABX-002\\\"," +
                        "\\\"title\\\":\\\"How to make Android APK in 2 seconds :)\\\"" +
                        "}" +
                        "]\"," +
                        "\"headers\":{\"Server\":\"Apache-Coyote/1.1\"," +
                                    "\"Content-Type\":\"text/xml\"," +
                                    "\"Content-Length\":\"131\"," +
                                    "\"Date\":\"Thu, 06 Sep 2012 09:10:26 GMT\"" +
                                    "}" +
                        "}";

I want to parse the Content-Type Key . So I have written the below code to parse the value:

var jsonResponse = jQuery.parseJSON(response1); 
var contentType  = jsonResponse.headers.Content-Type;

I am not able to get the value of Content-Type and Content-Length. Any Help would be appreciated. Thanks a lot

Community
  • 1
  • 1
s_k_t
  • 689
  • 1
  • 15
  • 36

1 Answers1

5

When the key is not a legal token, you must use a string as the key and use array syntax:

var contentType  = jsonResponse.headers['Content-Type'];

NB: this is not a "JSON parsing" issue, it's standard Javascript object access rules.

Alnitak
  • 334,560
  • 70
  • 407
  • 495