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

标签 javascript arrays json wordpress

您好,我有问题,我尝试过类似的事情并回答,但 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>

这返回 1445 的长度而不是我需要的 5,如何获取 JSON 中的段数如何获取 5 而不是字符数?

你能帮帮我吗?谢谢。

最佳答案

您需要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()而 which 是上面的简写版本。

关于javascript - javascript中的JSON长度返回字符数而不是数组元素数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38192517/

相关文章:

MySQL返回数组中的打印两次

c++ - 字符串数组到字符串类型 vector 的转换

javascript - JSON 中所需的帮助

json - 从 Flash AS3 发送 Json 表单

javascript - 将用户输入的原始 JSON 输入到 JSON 对象中

javascript - 显示我拍摄的照片的缩略图-Phonegap

javascript - 当索引满足另一个条件时,查找Javascript数组元素的值和索引

javascript - 使用 JavaScript 打印多个 PDF 文件

javascript - 如何使用 jasmine javascript 获取 selenium webdriver 中的 xpath 计数?

java - 带有版本控制的 json 数据存储