2016-07-04 59 views
1

嗨我有問題,我嘗試了類似的東西,並回答,但沒有Object.keys(數據)。長度不幫助我。這裏是我的JSON在JavaScript中的JSON長度返回字符數,而不是數組元素數

{ 
    "POSTS": [ 
     [ 
      { 
       "term_id": 1, 
       "name": "Uncategorized", 
       "slug": "uncategorized", 
       "term_group": 0, 
       "term_taxonomy_id": 1, 
       "taxonomy": "category", 
       "description": "", 
       "parent": 0, 
       "count": 1, 
       "filter": "raw", 
       "cat_ID": 1, 
       "category_count": 1, 
       "category_description": "", 
       "cat_name": "Uncategorized", 
       "category_nicename": "uncategorized", 
       "category_parent": 0 
      }, 
      { 
       "term_id": 2, 
       "name": "Nova", 
       "slug": "nova", 
       "term_group": 0, 
       "term_taxonomy_id": 2, 
       "taxonomy": "category", 
       "description": "", 
       "parent": 0, 
       "count": 1, 
       "filter": "raw", 
       "cat_ID": 2, 
       "category_count": 1, 
       "category_description": "", 
       "cat_name": "Nova", 
       "category_nicename": "nova", 
       "category_parent": 0 
      }, 
      { 
       "term_id": 3, 
       "name": "nova1", 
       "slug": "nova1", 
       "term_group": 0, 
       "term_taxonomy_id": 3, 
       "taxonomy": "category", 
       "description": "", 
       "parent": 0, 
       "count": 1, 
       "filter": "raw", 
       "cat_ID": 3, 
       "category_count": 1, 
       "category_description": "", 
       "cat_name": "nova1", 
       "category_nicename": "nova1", 
       "category_parent": 0 
      }, 
      { 
       "term_id": 4, 
       "name": "nova2", 
       "slug": "nova2", 
       "term_group": 0, 
       "term_taxonomy_id": 4, 
       "taxonomy": "category", 
       "description": "", 
       "parent": 3, 
       "count": 1, 
       "filter": "raw", 
       "cat_ID": 4, 
       "category_count": 1, 
       "category_description": "", 
       "cat_name": "nova2", 
       "category_nicename": "nova2", 
       "category_parent": 3 
      }, 
      { 
       "term_id": 5, 
       "name": "nova3", 
       "slug": "nova3", 
       "term_group": 0, 
       "term_taxonomy_id": 5, 
       "taxonomy": "category", 
       "description": "", 
       "parent": 3, 
       "count": 1, 
       "filter": "raw", 
       "cat_ID": 5, 
       "category_count": 1, 
       "category_description": "", 
       "cat_name": "nova3", 
       "category_nicename": "nova3", 
       "category_parent": 3 
      } 
     ] 
    ], 
    "success": 1 
} 

這裏是我的javascript代碼:

<select id ="new" name="new"></select> 

<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script> 

<script type="text/javascript"> 
    $(document).ready(function(e) { 
     var select = document.getElementById("new"); 
     $.ajax({ 
      type: "POST", 
      url: "wp-content/themes/twentyfourteen/ajax.php", // this script returns my JSON 
      data: ({canum: 0}), 
      success: function(data){ 
       console.log(Object.keys(data).length); 
      } 
     }); 
    }); 
</script> 

這將返回我1445的長度不是5,因爲我需要,如何讓JSON段數如何得到5,而不是的字符數量?

你能幫助我嗎?謝謝。

+0

你的json只是一個字符串,直到你解析它,對嗎? http://api.jquery.com/jquery.parsejson/ – Nick

回答

6

您需要JSON.parse的數據。在你的情況下,它不是作爲application/json發送的,而是作爲text/html發送的。

您的成功,函數應該做這樣的事情:

success: function(data){ 
    data = JSON.parse(data); 
    console.log(Object.keys(data).length); 
} 

作爲替代方案, 你雖然可以試試這個,如果你不想JSON.parse數據,只是有jQuery的管理它。您可以通過dataType你期望從服務器返回的在你的配置對象:

$.ajax({ 
    type: "POST", 
    url: "wp-content/themes/twentyfourteen/ajax.php", // this script returns my JSON 
    data: ({canum: 0}), 
    success: function (data) { 
     console.log(Object.keys(data).length); 
    }, 
    dataType: 'json' // <- Add this 
}); 

這裏是談論該鍵的選項documentation for $.ajax


而作爲一個最後的選擇,可以使用$.getJson()反而是上面的簡寫版本。

+1

正如這個人所說...解析它的最簡單方法是隻要將'dataType:'json''添加到ajax請求中。 –

+0

@KevBot非常感謝我打開我的android應用程序的那一刻我記得我需要進行解析。感謝您的解答和答案。 –

相關問題