Javascript JSON 响应数据中的数据长度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11081148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Data length in JSON response data
提问by user1077220
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?
我预计长度是 2。有什么解释吗?
$.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
哈维尔
回答by Raab
data= JSON.parse(data);
console.log(data.length);
OR
或者
data= $.parseJSON(data);
console.log(data.length);
回答by Bogdan Emil Mariesan
The console.log will output you 95 because it interprets your dataobject as a string which in fact has exactly 95 characters.
console.log 将输出您 95,因为它将您的数据对象解释为一个字符串,而该字符串实际上正好有 95 个字符。
回答by U.P
because you are looking at the length of string. Try parsing it in JSoN object and see the length
因为您正在查看字符串的长度。尝试在 JSoN 对象中解析它并查看长度
$.parseJSON(data)