2011-06-15 52 views
0
{ 
    "code":100, 
    "data":{ 
     "month":[ 
     { 
      "yearText":"2011", 
      "months":[ 
       { 
        "monthText":"6", 
        "days":[ 
        { 
         "dayText":"13", 
         "cios":[ 
          { 
           "status":"continues", 
           "start":"23:00:00", 
           "end":"23:59:59", 
           "id":12 
          } 
         ], 
         "bois":[ 
          { 
           "status":"continues", 
           "start":"23:30:00", 
           "end":"23:59:59", 
           "id":12 
          } 
         ] 
        }, 
        { 
         "dayText":"14", 
         "cios":[ 
          { 
           "status":"continued", 
           "start":"00:00:00", 
           "end":"01:00:00", 
           "id":12 
          }, 
          { 
           "status":"within", 
           "start":"11:42:14", 
           "end":"11:43:45", 
           "id":11 
          } 
         ], 
         "bois":[ 
          { 
           "status":"continued", 
           "start":"00:00:00", 
           "end":"00:30:00", 
           "id":12 
          }, 
          { 
           "status":"within", 
           "start":"11:42:39", 
           "end":"11:43:33", 
           "id":11 
          } 
         ] 
        } 
        ] 
       } 
      ] 
     } 
     ], 
     "next":"\/attendance\/get-history\/2011\/07", 
     "previous":"\/attendance\/get-history\/2011\/05" 
    }, 
    "msg":"Attendance history of John Doe on June, 2011." 
} 

被zendframework生成此JSON輸出文件,我試圖訪問裏面的「月」的對象:通過jQuery的字符串,它看起來像一個數組。訪問JSON輸出由zendframework與jquery

我試圖訪問它們像通常的對象,如在month.yearText沒有運氣,我沒有做它的權利。

有幫助嗎?請。

感謝

+0

您可以通過選擇它,在'{}'按鈕點擊格式化你的代碼。 – 2011-06-15 08:58:24

+0

你已經說過了:*月份看起來像一個數組*。你如何訪問數組? – 2011-06-15 09:27:02

+0

@Felix Kling:那麼,Javascript是非常靈活的。因此,您可以使用month ['yearText']或month.yearText在有關聯數組時使用該值(請參見[Javascript - Objects as associative array](http://www.quirksmode.org/js/associative。 HTML))。 @hilarl:你可以發佈你正在使用的JS代碼,或者 - 不合格 - 你是否嘗試過使用Firebug來檢查你的假設是否正確? – wimvds 2011-06-15 11:02:55

回答

0

嘗試jQuery的parseJSON()功能

事情是這樣的:

var obj = jQuery.parseJSON('{"name":"John"}'); 
alert(obj.name === "John"); 
0

您需要使用month[0].yearText,因爲一個月陣列。