迭代嵌套的JSON对象

时间:2015-04-21 18:46:52

标签: javascript jquery json

我在尝试让嵌套对象循环时遇到了麻烦。问题是它甚至没有触及循环,就像没有值循环一样。我已经浏览了几个帖子并尝试了他们不得不说的内容,它甚至没有进入循环。下面是我正在使用的代码,任何帮助都会很棒。

// json string being parsed
var objString = '{ "error": "no crontab for ec2-user "}}{ "accounts" : { "demo": { "Name": "Your Medical Clinic", "groupid": "demo", "CallerID": "8664738160", "CallerID_err": "OK", "TransferPhone": "1-931-555-1212", "TransferPhone_err": "INVALID LENGTH", "TimeZone": "EST", "EMRSoftware": "Greenway", "merlin": 1, "prm": 1, "CallDaysOut": 2, "EmailDaysOut": 5, "enablevoice": 1, "enabletext": 1, "enablemail": 1, "Exclude": "12 Months,12-17 Years,18+ Years,2 Months,", "emailBtnConfirm": 1, "emailBtnReschedule": 1, "emailBtnCancel": 1, "enablenoshow": 1, "enablecollection": 0, "enablerecall": 1, "enableapptalert": 1, "AppendCallerID": "Confirm,Cancel,Resched,AnsMach", "EmailReport": "kevin@relatient.net,john@relatient.net,hello@gim.com", "EmailReport_err": "OK", "SurveyReport": "chris@relatient.net", "SurveyReport_err": "OK", "HMReport": "john@relatient.net,kevin@relatient.net", "HMReport_err": "OK", "ApptReminders" : { "cronCalls": { "cronCalls_570": { "StartHour": 18, "StartQtrHour": 0, "StartTime_local": "7:00 PM EST", "EndHour": 19, "EndQtrHour": 0, "EndTime_local": "8:00 PM EST", "CallDays": "MoTuWeThFr" } }, "cronEmail": { "cronEmail_110": { "hour": 18, "qtrhour": 99, "StartTime_local": "INVALID EST", "ApptTimeMath": "", "EmailDays": "MoTuWeThFr" } }, "cronSMS": { "cronSMS_82": { "hour": 7, "qtrhour": 0, "StartTime_local": "8:00 AM EST", "DaysOut": 0, "cronargid": 1, "cronargid_type": "frombulk", "ApptTimeMath": "", "WorkDays": "MoTuWeThFr", "SendOnDays": "TuWeThFr", "cronSMS_err": "OK" } } }, "HealthMaint" : { "cronHMCalls": { "cronHMCalls_2": { "StartHour": 12, "StartQtrHour": 0, "StartTime_local": "1:00 PM EST", "EndHour": 16, "EndQtrHour": 0, "EndTime_local": "5:00 PM EST", "ActiveDays": "MoTuWeFrSa" } }, "cronHMEmail": { "cronHMEmail_1": { "hour": 3, "qtrhour": 3, "StartTime_local": "4:45 AM EST", "ActiveDays": "ThFrSa" } }, "cronHMSMS": { "cronHMSMS_1": { "hour": 2, "qtrhour": 2, "StartTime_local": "3:30 AM EST", "ActiveDays": "SuTuThSa" } } }, "MDPay" : { "cronMDPayCalls": { "cronMDPayCalls_2": { "hour": 15, "qtrhour": 3, "StartTime_local": "4:45 PM EST", "ActiveDays": "MoWeFr" } }, "cronMDPayEmail": { "cronMDPayEmail_2": { "hour": 13, "qtrhour": 1, "StartTime_local": "2:15 PM EST", "ActiveDays": "MoFr" } }, "cronMDPaySMS": { "cronMDPaySMS_2": { "hour": 15, "qtrhour": 1, "StartTime_local": "4:15 PM EST", "ActiveDays": "Su" } } }, "NoShow" : { "cronNoShowCalls": { } }, "Recall" : { "cronRecallCalls": { "cronRecallCalls_4": { "Hour": 11, "QtrHour": 0, "StartTime_local": "12:00 PM EST", "ActiveDays": "MoTuWeThFr" } } }, "cronEmailReports": { "cronEmailReport_155": { "hour": 20, "qtrhour": 1, "StartTime_local": "9:15 PM EST", "DaysOut": 4, "csv": 1, "reportargid": 3, "reportargid_type": "calltime", "WorkDays": "SuMoTuThFrSa", "SendOnDays": "MoTuWeThFr" } }, "CallCntNotStatus99": 3680, "crontab": { }, "IncomingFiles": { "file_1": "payments.json", "file_1_changed": "2015-04-08 10:44", "file_1_lines": 222, "file_2": "recall.csv", "file_2_changed": "2014-04-17 08:58", "file_2_lines": 0, "file_3": "elerts.txt", "file_3_changed": "2014-12-11 14:14", "file_3_lines": 21, "file_4": "noshow.csv", "file_4_changed": "2014-04-17 08:44", "file_4_lines": 0, "file_5": "daily.txt", "file_5_changed": "2014-10-29 12:50", "file_5_lines": 188, "file_6": "bulk.csv", "file_6_changed": "2014-10-20 14:41", "file_6_lines": 1 } } }}';

通过对象循环的函数

// example code to loop through object
var jsontext = objString.replace('{ "error": "no crontab for ec2-user "}}', '').trim(),
    obj = JSON.parse(jsontext);

console.log(obj);

if(typeof obj !== 'object') console.log('error'); return false;

// top level loop
$.each(obj, lp);

function lp(key, val) {
    // render table
    console.log(key);
    console.log(val);

    // recursion for children
    if(val !== null && typeof val === "object") 
        $.each(val, lp);
}

Demo Fiddle

1 个答案:

答案 0 :(得分:8)

这是因为你的单行if声明。如果省略花括号,只有第一行是语句的一部分,所以每次都执行return false;

使用换行符可以看出错误:

if(typeof obj !== 'object') 
    console.log('error'); 

return false;

//Bunch of code that never gets hit
相关问题