admin管理员组

文章数量:1277317

the output of the code below is this:

[{"user_name":"Maria","user_surname":"Dominguez"},{"user_name":"Celia","user_surname":"Paris"}]
95

I expected the length be 2. Any explanation?

$.get(
    "/school/view-more-users",
    {"type": user_type, "school_id": school_id, "offset": offset},
    function(data){
        console.log(data);
        console.log(data.length);
        ,  
    "json"
);

Javier

the output of the code below is this:

[{"user_name":"Maria","user_surname":"Dominguez"},{"user_name":"Celia","user_surname":"Paris"}]
95

I expected the length be 2. Any explanation?

$.get(
    "http://myfirm.local/school/view-more-users",
    {"type": user_type, "school_id": school_id, "offset": offset},
    function(data){
        console.log(data);
        console.log(data.length);
        ,  
    "json"
);

Javier

Share Improve this question edited Jun 18, 2012 at 10:48 Alex K. 176k32 gold badges274 silver badges295 bronze badges asked Jun 18, 2012 at 10:40 user1077220user1077220 1,5235 gold badges17 silver badges21 bronze badges 1
  • using $.each to deal with it, you don't need to know how many elements I think. – Tom Commented Jun 18, 2012 at 10:55
Add a ment  | 

3 Answers 3

Reset to default 4
data= JSON.parse(data);
console.log(data.length);

OR

data= $.parseJSON(data);
console.log(data.length);

The console.log will output you 95 because it interprets your data object as a string which in fact has exactly 95 characters.

because you are looking at the length of string. Try parsing it in JSoN object and see the length

$.parseJSON(data)

本文标签: javascriptData length in JSON response dataStack Overflow