2016-07-04 78 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应用程序的那一刻我记得我需要进行解析。感谢您的解答和答案。 –

相关问题