简体   繁体   English

为什么javascript对象“未定义”?

[英]why is javascript object “undefined”?

I have JSON coming from ajax into javascript. 我有从ajax到javascript的JSON。 When I try to reference two properties of the object, it says they are undefined. 当我尝试引用该对象的两个属性时,它说它们是未定义的。 Why? 为什么?

 data="{\\"headers\\":[{\\"Name\\":\\"Margin-G\\",\\"Description\\":\\"Interactive Brokers Margin Account\\",\\"Value\\":\\"82632\\",\\"GrowthRate\\":\\"5\\",\\"PayoutRate\\":\\"4\\",\\"Contribution\\":\\"0\\",\\"TableName\\":\\"Account\\"},{\\"Name\\":\\"IRA-G\\",\\"Description\\":\\"Interactive Brokers IRA\\",\\"Value\\":\\"272607\\",\\"GrowthRate\\":\\"5\\",\\"PayoutRate\\":\\"4\\",\\"Contribution\\":\\"6500\\",\\"TableName\\":\\"Account\\"},{\\"Name\\":\\"Roth-G\\",\\"Description\\":\\"Vanguard Roth IRA\\",\\"Value\\":\\"69149\\",\\"GrowthRate\\":\\"5\\",\\"PayoutRate\\":\\"4\\",\\"Contribution\\":\\"0\\",\\"TableName\\":\\"Account\\"},{\\"Name\\":\\"DRIP-A\\",\\"Description\\":\\"Dividend Reinvestment stocks\\",\\"Value\\":\\"3825\\",\\"GrowthRate\\":\\"5\\",\\"PayoutRate\\":\\"4\\",\\"Contribution\\":\\"0\\",\\"TableName\\":\\"Account\\"},{\\"Name\\":\\"Roth 401K\\",\\"Description\\":\\"Everbank Foreign Currency\\",\\"Value\\":\\"17246\\",\\"GrowthRate\\":\\"5\\",\\"PayoutRate\\":\\"4\\",\\"Contribution\\":\\"0\\",\\"TableName\\":\\"Account\\"},{\\"Name\\":\\"Env 401K\\",\\"Description\\":\\"Enventive 401K\\",\\"Value\\":\\"22434\\",\\"GrowthRate\\":\\"5\\",\\"PayoutRate\\":\\"4\\",\\"Contribution\\":\\"24000\\",\\"TableName\\":\\"Account\\"},{\\"Name\\":\\"IRA-A\\",\\"Description\\":\\"IRA-A\\",\\"Value\\":\\"90094\\",\\"GrowthRate\\":\\"5\\",\\"PayoutRate\\":\\"4\\",\\"Contribution\\":\\"0\\",\\"TableName\\":\\"Account\\"},{\\"Name\\":\\"ULA 401K\\",\\"Description\\":\\"ULA 401K\\",\\"Value\\":\\"219421\\",\\"GrowthRate\\":\\"5\\",\\"PayoutRate\\":\\"4\\",\\"Contribution\\":\\"0\\",\\"TableName\\":\\"Account\\"},{\\"Name\\":\\"Roth-A\\",\\"Description\\":\\"Roth-Annette\\",\\"Value\\":\\"14658\\",\\"GrowthRate\\":\\"5\\",\\"PayoutRate\\":\\"4\\",\\"Contribution\\":\\"0\\",\\"TableName\\":\\"Account\\"},{\\"Name\\":\\"Pera 401K\\",\\"Description\\":\\"Pera 401K\\",\\"Value\\":\\"28448\\",\\"GrowthRate\\":\\"5\\",\\"PayoutRate\\":\\"4\\",\\"Contribution\\":\\"0\\",\\"TableName\\":\\"Account\\"},{\\"Name\\":\\"Pera Roth 401K\\",\\"Description\\":\\"Pera Roth 401K\\",\\"Value\\":\\"4075\\",\\"GrowthRate\\":\\"5\\",\\"PayoutRate\\":\\"4\\",\\"Contribution\\":\\"0\\",\\"TableName\\":\\"Account\\"},{\\"Name\\":\\"Pera 457 Roth\\",\\"Description\\":\\"Pera 457 Roth\\",\\"Value\\":\\"7335\\",\\"GrowthRate\\":\\"5\\",\\"PayoutRate\\":\\"4\\",\\"Contribution\\":\\"0\\",\\"TableName\\":\\"Account\\"},{\\"Name\\":\\"SS-A\\",\\"Description\\":\\"Social Security Annette\\",\\"Value\\":\\"28416\\",\\"GrowthRate\\":\\"0\\",\\"PayoutRate\\":\\"0\\",\\"Contribution\\":\\"1\\",\\"TableName\\":\\"Income\\"},{\\"Name\\":\\"SS-G\\",\\"Description\\":\\"Social Security Greg\\",\\"Value\\":\\"21756\\",\\"GrowthRate\\":\\"1\\",\\"PayoutRate\\":\\"0\\",\\"Contribution\\":\\"1\\",\\"TableName\\":\\"Income\\"}],\\"years\\":[[2018,82632,0,272607,0,69149,0,3825,0,17246,0,22434,0,90094,0,219421,0,14658,0,28448,0,4075,0,7335,0,28416,21756,50172],[2019,86764,0,292737,0,72606,0,4016,0,18108,0,47556,0,94599,0,230392,0,15391,0,29870,0,4279,0,7702,0,28416,21756,50172],[2020,91102,0,313874,0,76236,0,4217,0,19013,0,73934,0,99329,0,241912,0,16161,0,31364,0,4493,0,8087,0,28416,21756,50172],[2021,95657,0,336068,0,80048,0,4428,0,19964,0,101631,0,104295,0,254008,0,16969,0,32932,0,4718,0,8491,0,28416,21756,50172],[2022,100440,0,359371,0,84050,0,4649,0,20962,0,130713,0,109510,0,266708,0,17817,0,34579,0,4954,0,8916,0,28416,21756,50172],[2023,105462,0,383840,0,88253,0,4881,0,22010,0,161249,0,114986,0,280043,0,18708,0,36308,0,5202,0,9362,0,28416,21756,50172],[2024,110735,4429,403032,16121,92666,3707,5125,205,23111,924,169311,6772,120735,4829,294045,11762,19643,786,38123,1525,5462,218,9830,393,28416,21756,101845],[2025,111843,4474,407063,16283,93592,3744,5176,207,23343,934,171005,6840,121943,4878,296985,11879,19839,794,38504,1540,5517,221,9929,397,28416,21756,102362],[2026,112961,4518,411133,16445,94528,3781,5228,209,23576,943,172715,6909,123162,4926,299955,11998,20037,801,38889,1556,5572,223,10028,401,28416,21756,102883],[2027,114091,4564,415245,16610,95473,3819,5280,211,23812,952,174442,6978,124394,4976,302955,12118,20238,810,39277,1571,5628,225,10128,405,28416,21756,103411],[2028,115232,4609,419397,16776,96428,3857,5333,213,24051,962,176186,7047,125638,5026,305985,12239,20440,818,39670,1587,5684,227,10229,409,28416,21756,103943],[2029,116385,4655,423591,16944,97392,3896,5387,215,24292,972,177948,7118,126894,5076,309045,12362,20644,826,40067,1603,5741,230,10331,413,28416,21756,104481],[2030,117549,4702,427827,17113,98366,3935,5441,218,24535,981,179727,7189,128163,5127,312135,12485,20850,834,40467,1619,5798,232,10435,417,28416,21756,105024],[2031,118724,4749,432105,17284,99349,3974,5495,220,24781,991,181524,7261,129444,5178,315257,12610,21059,842,40871,1635,5856,234,10540,422,28416,21756,105572],[2032,119911,4796,436426,17457,100342,4014,5550,222,25029,1001,183339,7334,130738,5230,318410,12736,21270,851,41280,1651,5915,237,10645,426,28416,21756,106126],[2033,121111,4844,440790,17632,101345,4054,5606,224,25279,1011,185172,7407,132045,5282,321595,12864,21483,859,41693,1668,5974,239,10751,430,28416,21756,106686],[2034,122323,4893,445198,17808,102358,4094,5662,226,25532,1021,187024,7481,133365,5335,324811,12992,21698,868,42110,1684,6034,241,10859,434,28416,21756,107251],[2035,123546,4942,449650,17986,103382,4135,5719,229,25788,1032,188894,7556,134698,5388,328060,13122,21915,877,42532,1701,6095,244,10968,439,28416,21756,107822],[2036,124781,4991,454147,18166,104416,4177,5776,231,26045,1042,190783,7631,136045,5442,331341,13254,22134,885,42958,1718,6156,246,11077,443,28416,21756,108398],[2037,126029,5041,458688,18348,105460,4218,5834,233,26305,1052,192691,7708,137405,5496,334654,13386,22356,894,43388,1736,6218,249,11188,448,28416,21756,108981],[2038,127289,5092,463274,18531,106515,4261,5893,236,26568,1063,194618,7785,138779,5551,338001,13520,22580,903,43821,1753,6280,251,11299,452,28416,21756,109569],[2039,128561,5142,467907,18716,107580,4303,5952,238,26833,1073,196564,7863,140167,5607,341381,13655,22806,912,44259,1770,6343,254,11412,456,28416,21756,110163],[2040,129847,5194,472586,18903,108656,4346,6012,240,27102,1084,198529,7941,141568,5663,344795,13792,23034,921,44702,1788,6406,256,11527,461,28416,21756,110763],[2041,131145,5246,477312,19092,109743,4390,6073,243,27373,1095,200514,8021,142983,5719,348243,13930,23265,931,45149,1806,6470,259,11642,466,28416,21756,111369],[2042,132456,5298,482086,19283,110840,4434,6134,245,27647,1106,202519,8101,144413,5777,351725,14069,23497,940,45600,1824,6535,261,11758,470,28416,21756,111980],[2043,133781,5351,486907,19476,111948,4478,6196,248,27923,1117,204544,8182,145857,5834,355242,14210,23732,949,46056,1842,6601,264,11876,475,28416,21756,112599],[2044,135119,5405,491776,19671,113067,4523,6258,250,28202,1128,206589,8264,147316,5893,358794,14352,23970,959,46517,1861,6667,267,11995,480,28416,21756,113223],[2045,136470,5459,496694,19868,114197,4568,6321,253,28484,1139,208654,8346,148789,5952,362382,14495,24210,968,46982,1879,6733,269,12115,485,28416,21756,113853],[2046,137835,5513,501661,20066,115339,4614,6384,255,28769,1151,210741,8430,150276,6011,366006,14640,24453,978,47452,1898,6801,272,12236,489,28416,21756,114490],[2047,139214,5569,506678,20267,116492,4660,6448,258,29056,1162,212848,8514,151779,6071,369666,14787,24698,988,47927,1917,6869,275,12359,494,28416,21756,115133],[2048,140606,5624,511745,20470,117657,4706,6512,260,29347,1174,214976,8599,153297,6132,373362,14934,24945,998,48406,1936,6937,277,12483,499,28416,21756,115783],[2049,142012,5680,516862,20674,118834,4753,6578,263,29640,1186,217126,8685,154830,6193,377096,15084,25194,1008,48890,1956,7007,280,12608,504,28416,21756,116439],[2050,143433,5737,522031,20881,120023,4801,6644,266,29936,1197,219297,8772,156379,6255,380867,15235,25446,1018,49379,1975,7077,283,12734,509,28416,21756,117102],[2051,144868,5795,527252,21090,121223,4849,6710,268,30236,1209,221490,8860,157943,6318,384675,15387,25700,1028,49873,1995,7148,286,12862,514,28416,21756,117771],[2052,146316,5853,532525,21301,122435,4897,6778,271,30539,1222,223705,8948,159522,6381,388522,15541,25957,1038,50372,2015,7219,289,12991,520,28416,21756,118447],[2053,147779,5911,537850,21514,123660,4946,6846,274,30844,1234,225942,9038,161117,6445,392407,15696,26217,1049,50876,2035,7291,292,13121,525,28416,21756,119130],[2054,149257,5970,543229,21729,124897,4996,6914,277,31152,1246,228201,9128,162728,6509,396331,15853,26479,1059,51385,2055,7364,295,13252,530,28416,21756,119820],[2055,150750,6030,548661,21946,126146,5046,6983,279,31464,1259,230483,9219,164355,6574,400295,16012,26744,1070,51899,2076,7437,297,13385,535,28416,21756,120516],[2056,152258,6090,554148,22166,127407,5096,7053,282,31778,1271,232788,9312,165999,6640,404298,16172,27011,1080,52418,2097,7512,300,13519,541,28416,21756,121220],[2057,153781,6151,559689,22388,128681,5147,7124,285,32096,1284,235115,9405,167659,6706,408341,16334,27282,1091,52942,2118,7588,304,13654,546,28416,21756,121930]]}" //console.log("back from getIncomeSchedule.php: data="+JSON.stringify(data)); data = JSON.parse(JSON.stringify(data)); console.log(data.headers); console.log(data.years); 

You don't parse the string of the data, you parse the actual data. 您不解析数据的字符串,而是解析实际的数据。 Try this: 尝试这个:

data = JSON.parse(data);

Instead of: 代替:

data = JSON.parse(JSON.stringify(data));

Then you'll be able to access the headers and all. 然后,您将能够访问标题和所有标题。

Change data = JSON.parse(JSON.stringify(data)); 更改data = JSON.parse(JSON.stringify(data)); to data = JSON.parse(data); data = JSON.parse(data);

 data="{\\"headers\\":[{\\"Name\\":\\"Margin-G\\",\\"Description\\":\\"Interactive Brokers Margin Account\\",\\"Value\\":\\"82632\\",\\"GrowthRate\\":\\"5\\",\\"PayoutRate\\":\\"4\\",\\"Contribution\\":\\"0\\",\\"TableName\\":\\"Account\\"},{\\"Name\\":\\"IRA-G\\",\\"Description\\":\\"Interactive Brokers IRA\\",\\"Value\\":\\"272607\\",\\"GrowthRate\\":\\"5\\",\\"PayoutRate\\":\\"4\\",\\"Contribution\\":\\"6500\\",\\"TableName\\":\\"Account\\"},{\\"Name\\":\\"Roth-G\\",\\"Description\\":\\"Vanguard Roth IRA\\",\\"Value\\":\\"69149\\",\\"GrowthRate\\":\\"5\\",\\"PayoutRate\\":\\"4\\",\\"Contribution\\":\\"0\\",\\"TableName\\":\\"Account\\"},{\\"Name\\":\\"DRIP-A\\",\\"Description\\":\\"Dividend Reinvestment stocks\\",\\"Value\\":\\"3825\\",\\"GrowthRate\\":\\"5\\",\\"PayoutRate\\":\\"4\\",\\"Contribution\\":\\"0\\",\\"TableName\\":\\"Account\\"},{\\"Name\\":\\"Roth 401K\\",\\"Description\\":\\"Everbank Foreign Currency\\",\\"Value\\":\\"17246\\",\\"GrowthRate\\":\\"5\\",\\"PayoutRate\\":\\"4\\",\\"Contribution\\":\\"0\\",\\"TableName\\":\\"Account\\"},{\\"Name\\":\\"Env 401K\\",\\"Description\\":\\"Enventive 401K\\",\\"Value\\":\\"22434\\",\\"GrowthRate\\":\\"5\\",\\"PayoutRate\\":\\"4\\",\\"Contribution\\":\\"24000\\",\\"TableName\\":\\"Account\\"},{\\"Name\\":\\"IRA-A\\",\\"Description\\":\\"IRA-A\\",\\"Value\\":\\"90094\\",\\"GrowthRate\\":\\"5\\",\\"PayoutRate\\":\\"4\\",\\"Contribution\\":\\"0\\",\\"TableName\\":\\"Account\\"},{\\"Name\\":\\"ULA 401K\\",\\"Description\\":\\"ULA 401K\\",\\"Value\\":\\"219421\\",\\"GrowthRate\\":\\"5\\",\\"PayoutRate\\":\\"4\\",\\"Contribution\\":\\"0\\",\\"TableName\\":\\"Account\\"},{\\"Name\\":\\"Roth-A\\",\\"Description\\":\\"Roth-Annette\\",\\"Value\\":\\"14658\\",\\"GrowthRate\\":\\"5\\",\\"PayoutRate\\":\\"4\\",\\"Contribution\\":\\"0\\",\\"TableName\\":\\"Account\\"},{\\"Name\\":\\"Pera 401K\\",\\"Description\\":\\"Pera 401K\\",\\"Value\\":\\"28448\\",\\"GrowthRate\\":\\"5\\",\\"PayoutRate\\":\\"4\\",\\"Contribution\\":\\"0\\",\\"TableName\\":\\"Account\\"},{\\"Name\\":\\"Pera Roth 401K\\",\\"Description\\":\\"Pera Roth 401K\\",\\"Value\\":\\"4075\\",\\"GrowthRate\\":\\"5\\",\\"PayoutRate\\":\\"4\\",\\"Contribution\\":\\"0\\",\\"TableName\\":\\"Account\\"},{\\"Name\\":\\"Pera 457 Roth\\",\\"Description\\":\\"Pera 457 Roth\\",\\"Value\\":\\"7335\\",\\"GrowthRate\\":\\"5\\",\\"PayoutRate\\":\\"4\\",\\"Contribution\\":\\"0\\",\\"TableName\\":\\"Account\\"},{\\"Name\\":\\"SS-A\\",\\"Description\\":\\"Social Security Annette\\",\\"Value\\":\\"28416\\",\\"GrowthRate\\":\\"0\\",\\"PayoutRate\\":\\"0\\",\\"Contribution\\":\\"1\\",\\"TableName\\":\\"Income\\"},{\\"Name\\":\\"SS-G\\",\\"Description\\":\\"Social Security Greg\\",\\"Value\\":\\"21756\\",\\"GrowthRate\\":\\"1\\",\\"PayoutRate\\":\\"0\\",\\"Contribution\\":\\"1\\",\\"TableName\\":\\"Income\\"}],\\"years\\":[[2018,82632,0,272607,0,69149,0,3825,0,17246,0,22434,0,90094,0,219421,0,14658,0,28448,0,4075,0,7335,0,28416,21756,50172],[2019,86764,0,292737,0,72606,0,4016,0,18108,0,47556,0,94599,0,230392,0,15391,0,29870,0,4279,0,7702,0,28416,21756,50172],[2020,91102,0,313874,0,76236,0,4217,0,19013,0,73934,0,99329,0,241912,0,16161,0,31364,0,4493,0,8087,0,28416,21756,50172],[2021,95657,0,336068,0,80048,0,4428,0,19964,0,101631,0,104295,0,254008,0,16969,0,32932,0,4718,0,8491,0,28416,21756,50172],[2022,100440,0,359371,0,84050,0,4649,0,20962,0,130713,0,109510,0,266708,0,17817,0,34579,0,4954,0,8916,0,28416,21756,50172],[2023,105462,0,383840,0,88253,0,4881,0,22010,0,161249,0,114986,0,280043,0,18708,0,36308,0,5202,0,9362,0,28416,21756,50172],[2024,110735,4429,403032,16121,92666,3707,5125,205,23111,924,169311,6772,120735,4829,294045,11762,19643,786,38123,1525,5462,218,9830,393,28416,21756,101845],[2025,111843,4474,407063,16283,93592,3744,5176,207,23343,934,171005,6840,121943,4878,296985,11879,19839,794,38504,1540,5517,221,9929,397,28416,21756,102362],[2026,112961,4518,411133,16445,94528,3781,5228,209,23576,943,172715,6909,123162,4926,299955,11998,20037,801,38889,1556,5572,223,10028,401,28416,21756,102883],[2027,114091,4564,415245,16610,95473,3819,5280,211,23812,952,174442,6978,124394,4976,302955,12118,20238,810,39277,1571,5628,225,10128,405,28416,21756,103411],[2028,115232,4609,419397,16776,96428,3857,5333,213,24051,962,176186,7047,125638,5026,305985,12239,20440,818,39670,1587,5684,227,10229,409,28416,21756,103943],[2029,116385,4655,423591,16944,97392,3896,5387,215,24292,972,177948,7118,126894,5076,309045,12362,20644,826,40067,1603,5741,230,10331,413,28416,21756,104481],[2030,117549,4702,427827,17113,98366,3935,5441,218,24535,981,179727,7189,128163,5127,312135,12485,20850,834,40467,1619,5798,232,10435,417,28416,21756,105024],[2031,118724,4749,432105,17284,99349,3974,5495,220,24781,991,181524,7261,129444,5178,315257,12610,21059,842,40871,1635,5856,234,10540,422,28416,21756,105572],[2032,119911,4796,436426,17457,100342,4014,5550,222,25029,1001,183339,7334,130738,5230,318410,12736,21270,851,41280,1651,5915,237,10645,426,28416,21756,106126],[2033,121111,4844,440790,17632,101345,4054,5606,224,25279,1011,185172,7407,132045,5282,321595,12864,21483,859,41693,1668,5974,239,10751,430,28416,21756,106686],[2034,122323,4893,445198,17808,102358,4094,5662,226,25532,1021,187024,7481,133365,5335,324811,12992,21698,868,42110,1684,6034,241,10859,434,28416,21756,107251],[2035,123546,4942,449650,17986,103382,4135,5719,229,25788,1032,188894,7556,134698,5388,328060,13122,21915,877,42532,1701,6095,244,10968,439,28416,21756,107822],[2036,124781,4991,454147,18166,104416,4177,5776,231,26045,1042,190783,7631,136045,5442,331341,13254,22134,885,42958,1718,6156,246,11077,443,28416,21756,108398],[2037,126029,5041,458688,18348,105460,4218,5834,233,26305,1052,192691,7708,137405,5496,334654,13386,22356,894,43388,1736,6218,249,11188,448,28416,21756,108981],[2038,127289,5092,463274,18531,106515,4261,5893,236,26568,1063,194618,7785,138779,5551,338001,13520,22580,903,43821,1753,6280,251,11299,452,28416,21756,109569],[2039,128561,5142,467907,18716,107580,4303,5952,238,26833,1073,196564,7863,140167,5607,341381,13655,22806,912,44259,1770,6343,254,11412,456,28416,21756,110163],[2040,129847,5194,472586,18903,108656,4346,6012,240,27102,1084,198529,7941,141568,5663,344795,13792,23034,921,44702,1788,6406,256,11527,461,28416,21756,110763],[2041,131145,5246,477312,19092,109743,4390,6073,243,27373,1095,200514,8021,142983,5719,348243,13930,23265,931,45149,1806,6470,259,11642,466,28416,21756,111369],[2042,132456,5298,482086,19283,110840,4434,6134,245,27647,1106,202519,8101,144413,5777,351725,14069,23497,940,45600,1824,6535,261,11758,470,28416,21756,111980],[2043,133781,5351,486907,19476,111948,4478,6196,248,27923,1117,204544,8182,145857,5834,355242,14210,23732,949,46056,1842,6601,264,11876,475,28416,21756,112599],[2044,135119,5405,491776,19671,113067,4523,6258,250,28202,1128,206589,8264,147316,5893,358794,14352,23970,959,46517,1861,6667,267,11995,480,28416,21756,113223],[2045,136470,5459,496694,19868,114197,4568,6321,253,28484,1139,208654,8346,148789,5952,362382,14495,24210,968,46982,1879,6733,269,12115,485,28416,21756,113853],[2046,137835,5513,501661,20066,115339,4614,6384,255,28769,1151,210741,8430,150276,6011,366006,14640,24453,978,47452,1898,6801,272,12236,489,28416,21756,114490],[2047,139214,5569,506678,20267,116492,4660,6448,258,29056,1162,212848,8514,151779,6071,369666,14787,24698,988,47927,1917,6869,275,12359,494,28416,21756,115133],[2048,140606,5624,511745,20470,117657,4706,6512,260,29347,1174,214976,8599,153297,6132,373362,14934,24945,998,48406,1936,6937,277,12483,499,28416,21756,115783],[2049,142012,5680,516862,20674,118834,4753,6578,263,29640,1186,217126,8685,154830,6193,377096,15084,25194,1008,48890,1956,7007,280,12608,504,28416,21756,116439],[2050,143433,5737,522031,20881,120023,4801,6644,266,29936,1197,219297,8772,156379,6255,380867,15235,25446,1018,49379,1975,7077,283,12734,509,28416,21756,117102],[2051,144868,5795,527252,21090,121223,4849,6710,268,30236,1209,221490,8860,157943,6318,384675,15387,25700,1028,49873,1995,7148,286,12862,514,28416,21756,117771],[2052,146316,5853,532525,21301,122435,4897,6778,271,30539,1222,223705,8948,159522,6381,388522,15541,25957,1038,50372,2015,7219,289,12991,520,28416,21756,118447],[2053,147779,5911,537850,21514,123660,4946,6846,274,30844,1234,225942,9038,161117,6445,392407,15696,26217,1049,50876,2035,7291,292,13121,525,28416,21756,119130],[2054,149257,5970,543229,21729,124897,4996,6914,277,31152,1246,228201,9128,162728,6509,396331,15853,26479,1059,51385,2055,7364,295,13252,530,28416,21756,119820],[2055,150750,6030,548661,21946,126146,5046,6983,279,31464,1259,230483,9219,164355,6574,400295,16012,26744,1070,51899,2076,7437,297,13385,535,28416,21756,120516],[2056,152258,6090,554148,22166,127407,5096,7053,282,31778,1271,232788,9312,165999,6640,404298,16172,27011,1080,52418,2097,7512,300,13519,541,28416,21756,121220],[2057,153781,6151,559689,22388,128681,5147,7124,285,32096,1284,235115,9405,167659,6706,408341,16334,27282,1091,52942,2118,7588,304,13654,546,28416,21756,121930]]}" //console.log("back from getIncomeSchedule.php: data="+JSON.stringify(data)); data = JSON.parse(data); console.log(data.headers); console.log(data.years); 

Please check out what step is doing wrong. 请检查出错误的步骤。

PS Don't use the same variable name with different data type even JavaScript supports. PS即使JavaScript支持,也不要对不同的数据类型使用相同的变量名。

console.log(data); // it should be an object.

var json_str = JSON.stringify(data);
console.log(json_str); // it should be json string

var obj = JSON.parse(json_str); // it should be an object as data
console.log(obj); // it should be an object as data

var headers = obj.headers; // it should exist
var years = obj.years; // it should exist

Your first call to JSON.stringify(data) expects that data to be an object, but it's declared as a string. 您对JSON.stringify(data)的首次调用期望该数据是一个对象,但是将其声明为字符串。

To declare as an object it should be something like: 要声明为一个对象,它应该类似于:

data = { "headers": [{ "Name":"Margin-G\"}] }

So in summary: 因此,总而言之:

  • If you receive string use JSON.parse() to convert it to object 如果收到字符串,请使用JSON.parse()将其转换为对象
  • If you need to display an object as JSON use JSON.stringify() 如果需要将对象显示为JSON,请使用JSON.stringify()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM