最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JSON length in javascript returns number of character instead of number of array elements - Stack Overflow

programmeradmin1浏览0评论

Hi I have problem and I tried similar things and answer but neither Object.keys(data).length doesn't help me. Here is my 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
}

And here is my javascript code :

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

<script type="text/javascript" src=".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>

This returns me 1445 as length not 5 as I need, how to get number of segments in JSON how to get 5 instead of number of characters ?

Can you help me please ? Thank you.

Hi I have problem and I tried similar things and answer but neither Object.keys(data).length doesn't help me. Here is my 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
}

And here is my javascript code :

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

<script type="text/javascript" src="http://code.jquery./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>

This returns me 1445 as length not 5 as I need, how to get number of segments in JSON how to get 5 instead of number of characters ?

Can you help me please ? Thank you.

Share Improve this question edited Jul 4, 2016 at 22:35 Alex K 7,2679 gold badges43 silver badges64 bronze badges asked Jul 4, 2016 at 21:37 cfhpanteracfhcfhpanteracfh 1153 silver badges10 bronze badges 1
  • Your json will just be a string until you parse it, right? api.jquery./jquery.parsejson – Nick Commented Jul 4, 2016 at 21:38
Add a ment  | 

1 Answer 1

Reset to default 7

You need to JSON.parse the data. In your case it is not sent as application/json, it is being sent as text/html.

Your success function should do something like this:

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

As an alternative, you can try this though if you do not want to JSON.parse the data and just have jQuery manage it. You can pass the dataType you are expecting back from the server in your config object:

$.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
});

Here is the documentation for $.ajax which talks about the options for that key.


And as one last option, you can use $.getJson() instead which is a shorthand version of the above.

发布评论

评论列表(0)

  1. 暂无评论