json格式如下:
[{"datetime":"2012-11-16","content":"INFORMATION RECEIVED (This is not an acknowledgment of the physical receipt of the stated Registered Article)","location":""},
{"datetime":"2012-11-17","content":"Despatched to overseas (Country code: SE)","location":""},{
"datetime":"2012-11-17","content":"Despatched to overseas (Country code: SE)","location":""},
{"datetime":"2012-11-20","content":"Arrived at overseas (Country code: SE)","location":""},
{"datetime":"2012-11-20","content":"Arrived at overseas (Country code: SE)","location":""},
{"datetime":"2012-11-26","content":"Attempted Delivery (Country code: SE)","location":""},
{"datetime":"2012-11-29","content":"Product Delivered (Country code: SE)","location":""}]但是打印出来却是这样子的:
0 2012-11-16 INFORMATION RECEIVED (This is not an acknowledgment of the physical receipt of the stated Registered Article) 
1 2012-11-17 Despatched to overseas (Country code: SE) 
2 2012-11-17 Despatched to overseas (Country code: SE) 
3 2012-11-20 Arrived at overseas (Country code: SE) 
4 2012-11-20 Arrived at overseas (Country code: SE) 
5 2012-11-26 Attempted Delivery (Country code: SE) 
6 2012-11-29 Product Delivered (Country code: SE) 
each undefined undefined undefined
eachSlice undefined undefined undefined
all undefined undefined undefined
any undefined undefined undefined
collect undefined undefined undefined
detect undefined undefined undefined
findAll undefined undefined undefined
select undefined undefined undefined
grep undefined undefined undefined
include undefined undefined undefined
member undefined undefined undefined
inGroupsOf undefined undefined undefined
inject undefined undefined undefined
invoke undefined undefined undefined
max undefined undefined undefined
min undefined undefined undefined
partition undefined undefined undefined
pluck undefined undefined undefined
reject undefined undefined undefined
sortBy undefined undefined undefined
toArray undefined undefined undefined
entries undefined undefined undefined
zip undefined undefined undefined
size undefined undefined undefined
inspect undefined undefined undefined
find undefined undefined undefined
_reverse undefined undefined undefined
_each undefined undefined undefined
clear undefined undefined undefined
first undefined undefined undefined
last undefined undefined undefined
compact undefined undefined undefined
flatten undefined undefined undefined
without undefined undefined undefined
uniq undefined undefined undefined
intersect undefined undefined undefined
clone undefined undefined undefined不明白这些undefined是从哪来的
我读取JSON的代码是
var data = eval("("+tracking+")");

var strData = "";

var srcEvent = data['srcEvent'];
for(var key in srcEvent){

    strData = strData +key+' '+srcEvent[key]['datetime']+' '+srcEvent[key]['content']+' '+srcEvent[key]['location']+'\n';

}

解决方案 »

  1.   

    敢贴完整点的代码不?  你的 tracking  是啥?  很明显 data['srcEvent'];里面有不只是你上面说的JSON内容
      

  2.   

    这个是tracking内容:
    {"mailNo":"RF110584998SG","mailType":"挂号信","lang":"cn","mstatus":"3","mailStatus":"成功签收","qstatus":"1","queryStatus":"正常","waittime":3984,"step":2,"srcId":"SG","dstId":"SE","srcMode":"0","dstMode":"0","srcName":"新加坡","dstName":"瑞典","mailDays":"14","lastEvent":{"datetime":"2012-11-29 8:40 PM","content":"The item has been consigned to the recipient","location":"ICA Nära Lindeborg"},"srcEvent":[{"datetime":"2012-11-16","content":"INFORMATION RECEIVED (This is not an acknowledgment of the physical receipt of the stated Registered Article)","location":""},{"datetime":"2012-11-17","content":"Despatched to overseas (Country code: SE)","location":""},{"datetime":"2012-11-17","content":"Despatched to overseas (Country code: SE)","location":""},{"datetime":"2012-11-20","content":"Arrived at overseas (Country code: SE)","location":""},{"datetime":"2012-11-20","content":"Arrived at overseas (Country code: SE)","location":""},{"datetime":"2012-11-26","content":"Attempted Delivery (Country code: SE)","location":""},{"datetime":"2012-11-29","content":"Product Delivered (Country code: SE)","location":""}],"dstEvent":[{"datetime":"2012-11-17 10:04 PM","content":"The item has been dispatched from the sorting terminal abroad for onward transport to Sweden","location":"SINGAPORE"},{"datetime":"2012-11-20 11:37 AM","content":"The item has arrived from abroad to Posten´s international terminal for sorting","location":"Stockholm utr"},{"datetime":"2012-11-20 11:37 AM","content":"Under customs clearance at Posten AB","location":"Stockholm utr"},{"datetime":"2012-11-21 4:33 PM","content":"Customs clearance underway","location":"Stockholm utr"},{"datetime":"2012-11-23 3:29 PM","content":"Customs clearance underway","location":"Stockholm utr"},{"datetime":"2012-11-24 3:06 PM","content":"The item will be dispatched onwards for delivery to the recipient","location":"Stockholm utr"},{"datetime":"2012-11-26 8:07 AM","content":"The item has been redirected to the recipient´s delivery point","location":"Posten Malmö Axel Danielssonsv"},{"datetime":"2012-11-26 11:55 AM","content":"The item has arrived at the recipient´s delivery point. Express to be dispatched to the recipient, other items notified","location":"ICA Nära Lindeborg"},{"datetime":"2012-11-29 8:40 PM","content":"The item has been consigned to the recipient","location":"ICA Nära Lindeborg"}],"srcLink":"http://www.singpost.com/index.php","dstLink":"http://www.posten.se/tracktrace/TrackConsignments_do.jsp?lang=GB"}
      

  3.   

    根据你提供的代码和数据,打印出来 strData 为:
    0 2012-11-16 INFORMATION RECEIVED (This is not an acknowledgment of the physical receipt of the stated Registered Article) 
    1 2012-11-17 Despatched to overseas (Country code: SE) 
    2 2012-11-17 Despatched to overseas (Country code: SE) 
    3 2012-11-20 Arrived at overseas (Country code: SE) 
    4 2012-11-20 Arrived at overseas (Country code: SE) 
    5 2012-11-26 Attempted Delivery (Country code: SE) 
    6 2012-11-29 Product Delivered (Country code: SE) 里面没有你上面贴出的东东 
    感觉你后面那些东东 应该是srcEvent  可能你有用到什么库或者插件啥的 把一些事件放到了这个变量里面 你可以改下这个变量名  如果猜得不错后面的那串东东应该不会再出现 另外  你单独写个文件 拿你上面贴出来的代码 测试下 应该不会出现那些乱七八糟的东东   个人认为 没有啥灵异事件  肯定是个人程序漏洞
      

  4.   

    试一下,用 hasOwnProperty 过滤一下 for loop:for(var key in srcEvent) if (srcEvent.hasOwnProperty(key)) {
        strData = strData +key+' '+srcEvent[key]['datetime']+' '
                  +srcEvent[key]['content']+' '+srcEvent[key]['location']+'\n';
    }
      

  5.   

    嗯,将框架对Array的prototype扩展列完出来了,这个也是常说对污染了原型,象楼上的操作可以,
    更方便的是直接用eachsrcEvent.each(function(item,index){
        strData = strData +index+' '+item['datetime']+' '+item['content']+' '+item['location']+'\n';})
      

  6.   

    循环用这种
    for(var key = 0 ; key < srcEvent.length ; ++key){
        strData = strData +key+' '+srcEvent[key]['datetime']+' '+srcEvent[key]['content']+' '+srcEvent[key]['location']+'\n';}
    或者加个hasOwnProperty判断一下