Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript中的JSON长度返回字符数,而不是数组元素数_Javascript_Arrays_Json_Wordpress - Fatal编程技术网

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

javascript中的JSON长度返回字符数,而不是数组元素数,javascript,arrays,json,wordpress,Javascript,Arrays,Json,Wordpress,嗨,我有问题,我尝试过类似的方法并给出了答案,但Object.keys(data.length)对我都没有帮助。这是我的JSON { "POSTS": [ [ { "term_id": 1, "name": "Uncategorized", "slug": "uncategorized", "term_group": 0,

嗨,我有问题,我尝试过类似的方法并给出了答案,但Object.keys(data.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
}
下面是我的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>

$(文档).ready(函数(e){
var select=document.getElementById(“新”);
$.ajax({
类型:“POST”,
url:“wp-content/themes/twentyreeven/ajax.php”//此脚本返回我的JSON
数据:({canum:0}),
成功:功能(数据){
console.log(Object.key(data.length));
}
});
});
这将返回1445作为长度,而不是我需要的5,如何获取
JSON中的段数
如何获取5而不是字符数


你能帮我吗?谢谢。

您需要
JSON.解析数据。在您的情况下,它不是作为
application/json
发送的,而是作为
text/html
发送的

您的成功功能应该执行以下操作:

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

作为替代方案, 但是,如果您不想
JSON.parse
数据并让jQuery管理它,您可以尝试这样做。您可以在config对象中从服务器传递所需的
数据类型

$.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
});
$.ajax({
类型:“POST”,
url:“wp-content/themes/twentyreeven/ajax.php”//此脚本返回我的JSON
数据:({canum:0}),
成功:功能(数据){
console.log(Object.key(data.length));
},

dataType:'json'//在你解析之前,你的json将只是一个字符串,对吗?正如这家伙所说的……解析它最简单的方法就是在ajax请求中添加
dataType:'json'
。@KevBot非常感谢你在我打开android应用程序的那一刻,我想起我需要进行解析。谢谢你的解决方案和答案。