我有这段代码:
$.getJSON('newMessageDetails', function (json)
{
var old_id = document.getElementById("td_id").value;
var messages_count = Object.keys(json).length;
console.log(messages_count);
console.log(json);
last_id = json[messages_count]["msgId"];
});
json [messages_count] [“msgId”]在控制台中给出了undefined ??
我的newMessageDetails:
public function executeNewMessageDetails(sfWebRequest $request)
{
$profile_id = $this->getUser()->getAttribute('profile_id','zero');
$new_msgs = RcMessageBoxTablePeer::getNewMessages($profile_id);
$hr=2;
$i=1;
if (count($new_msgs) >= 1)
{
foreach ($new_msgs as $row)
{
$date = $row->getCreatedAt();
//$cd = strtotime($date);
//$newdate = date('Y-m-d H:i:s', mktime(date('h',$cd), date('i',$cd), date('s',$cd), date('m',$cd), date('d',$cd), date('Y',$cd)));
$subject = $row->getSubject();
$message = $row->getMessage();
$from = $row->getProfileIdFrom();
$id = $row->getId();
$uc_record = RcProfileTablePeer::getById($from);
$uc_from = $uc_record->getUniqueCode();
$output[$i] = array("td_date" => $date, "td_subject" => $subject, "td_from" => $uc_from, "td_message" => $message, "msgId" => $id , "i" => $i);
$i++;
}
return $this->renderText(json_encode($output));
}
}
console.log(json)给出:
5
list:98
Object
543: Object
544: Object
545: Object
546: Object
547: Object
i: 1
msgId: 547
td_date: "2011-11-29 11:33:05"
td_from: "CHARLIE000RC"
td_message: "tooltip show message test 2 id 547"
td_subject: "Freechat message"
可以解释一下吗?不知道我做错了什么 感谢
答案 0 :(得分:1)
对象没有length属性。试试这个。
var obj = jQuery.parseJSON(json);
obj.length();
或者你可以试试这个。
Object.keys(json).length;
答案 1 :(得分:0)
如果您想要计算所有键,可以将其重写为:
$.getJSON('newMessageDetails', function (json)
{
...;
var messages_count = Object.keys(json).length;
console.log(messages_count);
});
答案 2 :(得分:0)
你应该遍历对象并计算。这是你真正知道对象中有多少“有意义”对象的唯一方法。