javascript中的JSON长度返回字符数而不是数组元素数

cfh*_*cfh 2 javascript arrays wordpress json

嗨,我有问题,我尝试过类似的事情并回答,但是Object.keys(数据).length都没有帮助我.这是我的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
}
Run Code Online (Sandbox Code Playgroud)

这是我的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>
Run Code Online (Sandbox Code Playgroud)

这将返回我1445作为长度不是5,因为我需要,如何让段数JSON如何得到5,而不是字符数?

你能帮我吗 ?谢谢.

Kev*_*Bot 6

你需要JSON.parse数据.在你的情况下,它不会被发送application/json,它被发送为text/html.

你的成功函数应该是这样的:

success: function(data){
    data = JSON.parse(data);
    console.log(Object.keys(data).length);
}
Run Code Online (Sandbox Code Playgroud)

作为替代方案, 您可以尝试这个,但如果您不想要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
});
Run Code Online (Sandbox Code Playgroud)

以下是有关该密钥选项的文档$.ajax.


作为最后一个选项,您可以使用$ .getJson()代替上面的简写版本.