Javascript对象 - 无法访问包含' - '的键

s_k*_*k_t 0 javascript jquery json

可能重复:
如何引用带有连字符的javascript对象属性?

我有一个以下格式的Json:

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\"" +
                                    "}" +
                        "}";
Run Code Online (Sandbox Code Playgroud)

我想解析Content-Type Key.所以我编写了以下代码来解析值:

var jsonResponse = jQuery.parseJSON(response1); 
var contentType  = jsonResponse.headers.Content-Type;
Run Code Online (Sandbox Code Playgroud)

我无法获得Content-Type和Content-Length的值.任何帮助,将不胜感激.非常感谢

Aln*_*tak 5

当密钥不是合法令牌时,您必须使用字符串作为密钥并使用数组语法:

var contentType  = jsonResponse.headers['Content-Type'];
Run Code Online (Sandbox Code Playgroud)

注意:这不是"JSON解析"问题,它是标准的Javascript对象访问规则.