访问json数组时在javascript中获取未定义

时间:2021-12-08 21:16:10

Getting problem to access json array.

获取访问json数组的问题。

this my javascript

这是我的javascript

function getCustomerInfo(){
    $.ajax({
    dataType: 'json',
    url:'https://host:8443/xxxxxx/xxx',
    type: 'POST',
        data: {requestor_email: 'honey@gmail.com'},
    success:function(data){
        alert(JSON.stringify(data)); //first alert
        alert(data.outputMap.customerName); //second alert
        alert(data.outputMap.emailId); //third alert
        alert(data.outputMap.orders); //fourth alert
        alert(data.outputMap.orders[0]); //fifth alert
    },
    error:function(data){
        alert(JSON.stringify(data));
        }
   });
}

My first alert prints the json response i.e

我的第一个警告打印出json响应,即

`

{
 "targetRequestUri":"/getCustomerInfo",
 "javax.servlet.request.key_size":128,
 "outputMap":
            {
             "emailId":"honey@gmail.com",
             "orders ":[
                       {
                        "orderId":"ST210340",
                        "orderDate":"2013-04-24 12:42:54.187",
                        "orderStatus":"ORDER_COMPLETED",
                        "totalMoney":1
                       }
                       ],
             "partyId ":"10810",
             "customerName":"honey goyal"
            },
 "_FORWARDED_FROM_SERVLET_":true,
 "javax.servlet.request.ssl_session":"519ee62b8656107fa3ac262c2ac8f86e0348933dc980e0078eca2fd638b55303",
 "javax.servlet.request.ssl_session_id":"519ee62b8656107fa3ac262c2ac8f86e0348933dc980e0078eca2fd638b55303",
 "_SERVER_ROOT_URL_":"https://host:8443",
 "javax.servlet.request.cipher_suite":"TLS_ECDHE_RSA_WITH_RC4_128_SHA",
 "thisRequestUri":"json"
}`

Second alert prints the correct customerName i.e honey goyal

第二个警报打印正确的客户名称,即honey goyal

Third alert prints the correct emailId i.e honey@gmail.com

第三个警告打印正确的emailId,即honey@gmail.com

Forth alert prints the undefined, this is the problem, it should prints [object Object]s.

Forth警告打印未定义,这是问题,它应该打印[object Object] s。

Fifth alert also responds the undefined.

第五个警报也响应未定义。

Thanks in advance.

提前致谢。

1 个解决方案

#1


2  

The correct key name for orders is "orders " (with a space at the end) as per the alerted JSON string.

根据警报的JSON字符串,订单的正确密钥名称是“orders”(末尾有空格)。

Try changing the fourth alert to: alert(data.outputMap['orders ']);

尝试将第四个警报更改为:alert(data.outputMap ['orders']);

#1


2  

The correct key name for orders is "orders " (with a space at the end) as per the alerted JSON string.

根据警报的JSON字符串,订单的正确密钥名称是“orders”(末尾有空格)。

Try changing the fourth alert to: alert(data.outputMap['orders ']);

尝试将第四个警报更改为:alert(data.outputMap ['orders']);