0

所以我有这个函数,只要客户端从它通过 websocket 连接的服务器收到消息,它就会运行:

ws.onmessage = function (event) {
  console.log("Data", JSON.parse(event.data)); // This line prints to the console and works as intended
  var res = JSON.parse(event.data); // This lines causes the errors
  switch (res.channel) {
    case 'exchangeRates':
      var exchangeRates = res.data;
      app.exchangeRates = exchangeRates;
    default:
      break;
  }
};

我没有得到的是,这将在我的浏览器控制台中显示(我正在运行最新版本的 firefox):

Data 
{…}
channel: "exchangeRates"
data: Object { btc: "14074.6", iot: "4.47305", eth: "427.009", … }
__proto__: Object { … }
app.js:43:3
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
[Learn More]

它试图解析的对象确实有 500 个值,所以它有点大。

任何人都可以尝试向我解释为什么 JSON.parse 在打印到控制台时有效但否则会导致错误...

更新:所以我尝试在 Chrome 上运行它,我收到了这个错误:

Uncaught SyntaxError: Unexpected token C in JSON at position 0
    at JSON.parse (<anonymous>)
    at WebSocket.ws.onmessage (app.js:43)

未解析的完整对象是以下字符串:

{"channel":"exchangeRates","data":{"btc":"14119.4","iot":"4.47606","eth":"427.173","bch":"1419.95","miota":"4.47606","xrp":"0.231809","dash":"706.628","ltc":"100.168","btg":"280.7","xmr":"280.705","ada":"0.116473","etc":"27.9304","xlm":"0.152531","xem":"0.268134","eos":"4.50441","neo":"35.0997","bcc":"364.628","lsk":"8.58936","zec":"337.898","mona":"16.5743","omg":"8.70191","qtum":"11.9674","waves":"8.38013","usdt":"1.00613","strat":"7.62121","ppt":"17.4642","hsr":"15.0566","emc2":"2.38926","ardr":"0.503933","nxt":"0.4639","bts":"0.175762","bcn":"0.00234004","ark":"4.20568","salt":"7.4472","vtc":"8.24051","rep":"30.7883","qash":"0.91154","steem":"1.27637","dcr":"49.7235","kmd":"2.93795","doge":"0.00257278","gnt":"0.336854","veri":"135.711","sc":"0.0082906","pivx":"4.71762","bnb":"2.55052","maid":"0.525271","pay":"2.24808","snt":"0.0653773","powr":"0.600372","wtc":"8.40793","knc":"1.53596","sys":"0.388583","bat":"0.204701","trx":"0.00306533","dgd":"99.3254","fct":"22.1313","btm":"1.10534","cnx":"4.2002","gbyte":"283.739","xuc":"7.21685","btcd":"134.25","xzc":"47.0386","ven":"0.574554","fun":"0.0374543","gas":"18.4407","icn":"1.57938","san":"2.55397","dgb":"0.0159688","pura":"0.854788","ae":"0.619127","gno":"129.431","rdn":"2.6627","mana":"0.0577787","rhoc":"0.708204","zrx":"0.256477","game":"1.97631","cvc":"0.352239","mtl":"6.22694","mco":"11.0955","block":"24.1746","data":"0.175062","nav":"1.89648","xvg":"0.00788486","ethos":"1.43434","gxs":"2.61426","bnt":"2.483","link":"0.286966","part":"12.848","etp":"2.66891","nxs":"1.80022","storj":"0.910689","act":"0.307483","sky":"15.2854","ubq":"2.28114","zen":"35.0199","sub":"0.379786","adx":"1.41594","xrb":"0.633468","edg":"1.02743","sngls":"0.138733","safex":"0.0377509","lkk":"0.297541","pot":"0.346827","bay":"0.0746919","ppc":"3.04434","grs":"1.08135","mnx":"24.4647","lrc":"0.249214","xcp":"26.7665","ant":"2.11018","r":"0.359665","fair":"1.24466","rcn":"0.133952","qrl":"1.24642","mer":"0.645461","req":"0.100423","mgo":"0.653578","qsp":"0.103724","yoyow":"0.297731","ftc":"0.337116","cob":"0.233737","atm":"0.0159596","tips":"0.000133817","wings":"0.638021","snm":"0.158329","aeon":"3.86596","via":"2.41473","drgn":"0.231579","mcap":"1.39211","nebl":"4.11046","rlc":"0.644786","ppp":"0.608341","vox":"0.233193","kin":"0.0000647149","kcs":"0.53713","jinn":"827.602","rise":"0.416216","rdd":"0.00162468","eng":"0.622553","ast":"0.3096","mln":"76.7965","cdt":"0.0811794","trst":"0.490039","emc":"1.08536","ioc":"2.7105","ink":"0.240771","xas":"0.471556","dct":"0.818068","cfi":"0.127176","otn":"9.48643","dmd":"16.0136","agrs":"0.964252","xwc":"0.160297","tnb":"0.034893","unity":"47.7905","sib":"2.39308","cloak":"7.36731","pepecash":"0.0533478","zsc":"0.0323658","ion":"2.02479","tnt":"0.086683","nmc":"2.47843","1st":"0.425694","bq":"0.0144683","mod":"2.06683","nlg":"0.0990707","lbc":"0.312304","gcr":"0.337744","taas":"4.19078","ok":"0.458312","plr":"0.147244","btx":"18.4111","amb":"0.228292","nlc2":"0.154277","mue":"0.303535","xaur":"0.245648","zny":"0.412465","sphr":"10.0499","dnt":"0.0510917","grid":"0.780742","ctr":"0.44982","grc":"0.0783","atb":"0.760273","cnd":"0.0204148","trig":"0.916718","leo":"0.30954","gup":"0.386337","tix":"0.716951","shift":"2.49187","xel":"0.324043","mda":"1.44015","blk":"0.365754","bbr":"2.58039","crw":"1.69675","csno":"0.393491","enj":"0.0359028","smart":"0.381232","edo":"2.48197","omni":"46.4706","nmr":"19.9809","payx":"0.376996","rby":"1.01419","avt":"4.21122","tkn":"1.05351","wct":"2.49338","sls":"24.5885","hmq":"0.151176","tx":"3.93951","amp":"0.29711","fuel":"0.0503226","wgr":"0.129652","vib":"0.14732","clam":"8.59589","icos":"39.8537","ukg":"0.172251","xnn":"0.076253","dcn":"0.0000693737","gvt":"5.88218","dpy":"0.771585","xdn":"0.00314379","plbt":"5.64138","uno":"108.087","golos":"0.170844","aur":"2.44991","ixt":"0.577987","stx":"0.482831","alis":"0.527277","rads":"5.94269","burst":"0.0110678","slr":"0.534782","dtb":"0.866601","hvn":"0.0518672","soar":"0.027216","poe":"0.00889508","evx":"1.16824","mth":"0.0884353","rpx":"0.0348584","xby":"0.0436985","oct":"0.62417","art":"0.61804","bdl":"0.103581","bsd":"1.03611","xrl":"0.209299","lmc":"0.0781097","adt":"0.0287825","vrc":"0.564047","neos":"4.76271","dlt":"0.19927","exp":"2.16424","snc":"0.158684","tcc":"0.102944","obits":"1.06301","dice":"2.38436","toa":"0.0124613","xvc":"1.0729","msp":"0.115578","prg":"0.248508","onion":"4.06891","swt":"2.01001","bco":"0.581989","b@":"1.5263","ppy":"4.0743","time":"21.5752","gam":"12.7641","ptoy":"0.218418","fldc":"0.0275909","nxc":"0.224743","tgt":"0.0156892","ift":"0.0779821","lun":"6.42298","flo":"0.104031","net":"1.37066","xto":"0.439764","enrg":"0.117068","btdx":"0.896309","bitcny":"0.134033","thc":"0.0601639","myst":"0.711079","pasc":"0.823281","pst":"0.253691","nsr":"0.00461754","drt":"0.0211859","heat":"0.406573","dbix":"7.6671","mtnc":"0.248668","poll":"1.73738","pzm":"1.05671","incnt":"0.261805","rvt":"0.456772","bitb":"0.00529677","nvc":"6.11245","blue":"0.341778","bmc":"0.379613","ric":"0.288127","oax":"0.455739","pink":"0.0305596","odn":"0.430966","eac":"0.000922463","cure":"0.445176","sbd":"2.97631","ac":"0.00903586","cag":"0.279183","seq":"0.228976","air":"0.00972768","xst":"0.365957","qau":"0.129482","bcy":"0.431767","bcpt":"0.214312","coss":"0.116432","moon":"0.0000430680","onx":"0.348273","kore":"4.69903","music":"0.0192828","nyc":"0.0000724249","hst":"0.299445","credo":"0.0167742","kick":"0.0284996","bitusd":"0.925214","iop":"4.84027","posw":"0.198881","dent":"0.000809449","dbet":"0.12932","xbc":"82.5178","xspec":"0.394376","xmy":"0.00530461","nvst":"0.539044","divx":"1.562","cat":"1.08837","plu":"9.1345","meme":"0.465214","coval":"0.00760537","qwark":"0.180259","apx":"10.7248","dyn":"3.98779","edr":"0.00839574","zrc":"2.03418","super":"0.156893","ptc":"0.0560056","aby":"0.00910712","excl":"1.64474","sxc":"0.0566603","csc":"0.182416","swift":"1.72547","synx":"0.42422","bet":"0.0419166","voise":"0.0108742","vibe":"0.0368846","cvcoin":"0.704415","put":"0.0219045","ecc":"0.00027162","gbx":"37.1838","astro":"2.04124","bis":"1.12523","snrg":"2.08004","pdc":"0.134859","tzc":"0.0816235","xc":"1.08648","phr":"0.65306","xpm":"0.289905","erc":"0.625541","drp":"0.767306","gld":"0.152452","crb":"0.375184","chc":"0.436794","trc":"0.282642","gcc":"0.0122009","la":"0.150185","trust":"0.186196","brx":"0.930394","dope":"0.0518159","bela":"0.146811","elix":"0.220522","pix":"0.0493769","html5":"0.00013581","trct":"1.21427","ecob":"0.0119566","2give":"0.0101859","pkb":"1.08906","krb":"1.06005","ntrn":"0.157953","blitz":"1.24117","brk":"0.279992","sss":"0.00309721","flixx":"0.072943","pirl":"0.996291","egc":"0.375236","unit":"0.407808","rup":"0.21873","blu":"0.00851589","btcred":"0.334042","dna":"0.131177","gb":"0.309749","pro":"0.331981","ind":"0.0981856","arn":"0.38669","ocl":"0.0963823","zcl":"2.40279","vsl":"0.128953","hush":"1.91084","cann":"0.053675","lux":"7.71958","nio":"0.062834","geo":"1.23605","cnt":"0.0555221","dovu":"0.122418","inxt":"6.47545","spr":"0.439088","dar":"0.296579","pbt":"3983.68","atms":"0.0396635","myb":"1.49361","tks":"3.42255","vrm":"2.72666","xsh":"0.00814656","crea":"0.293619","zeph":"0.0296523","dime":"0.0000068807","efl":"0.230851","start":"0.080808","real":"0.358371","bash":"0.00502977","qrk":"0.0140823","ping":"0.35278","tfl":"0.561258","mint":"0.000139233","dot":"0.0127586","mxt":"1.22064","proc":"0.0355738","huc":"0.165145","hgt":"0.0127884","ebtc":"0.177404","inn":"3.73085","hdg":"0.888512","atl":"0.248506","xmg":"0.43036","gmt":"0.0189332","vtr":"0.291992","rustbits":"0.0893892","scl":"0.18777","dft":"1.33885","kln":"4.88197","buzz":"0.000561828","hyp":"0.00269732","eqt":"0.291096","jns":"0.124341","pac":"0.0000008890","sta":"0.554767","vivo":"3.46151","flik":"0.0453539","xcn":"0.00728336","yoc":"0.0248949","hbt":"0.118605","log":"0.325944","adc":"0.00310777","btsr":"0.788992","adst":"0.215102","crave":"1.76088","cadastral":"0.08435","alt":"20.6006","pbl":"0.134159","prix":"2.02785","xp":"0.0000136375","ufo":"0.00067905","chips":"0.117883","adl":"0.0987881","pnd":"0.0000683770","zoi":"0.139984","fyp":"0.124595","xgox":"0.00354424","note":"0.0168489","mec":"0.0598096","visio":"0.0427366","pipl":"0.0254147","anc":"0.998394","wdc":"0.0179187","fcn":"0.339525","icoo":"4.505","wgo":"0.214893","ctx":"0.0341594","zeit":"0.0000559473","zeni":"0.00205877","opt":"0.0146001","ella":"0.756867","byc":"1.06665","fst":"0.0124657","lgd":"0.974114","kobo":"0.0781486","cdn":"0.0190962","life":"0.000278296","sumo":"0.744407","mbi":"0.107967","esp":"0.0000871841","ccrb":"0.506252","mag":"0.452519","hold":"0.0244518","mbrs":"0.0324332","rkc":"0.60053","dnr":"0.824814"}}

结果是如果我在初始化 res 后立即添加到该行:

console.log("Res", res);

它将正确打印解析 JSON 对象,但解析错误仍然显示并破坏我的代码......

4

0 回答 0