Gene Lim
Gene Lim

Reputation: 1068

Issue parsing JSON string containing dates

I am trying parse string to json. I received string from the server but i am always receiving error of unexpected token e

var data = JSON.parse(result)

result is

{"success":true,"data":[{"carID":100110,"teamID":0,"carNO":"carNO1","simNO":"1212","machineNO":"800704","controlPassword":null,"machineType":null,"protocol":7,"routeway":0,"carType":null,"carBrand":null,"carColor":null,"installPlace":"7","installPerson":null,"businessPerson":null,"joinTime":new UtcDate(1460357844353),"overServiceTime":new UtcDate(1491840000000),"carRemark":null,"driver":null,"driverTel":null,"driverMobile":null,"driver2":null,"driver2Tel":null,"driver2Mobile":null,"password":null,"driverAddress":null,"driverFax":null,"driverCompany":null,"buyTime":null,"stoped":0,"specialRequest":"0","driverRemark":null,"regionAlarm":0,"regionID":0,"positionID":0,"notify":0,"notifyStart":new UtcDate(1460357844353),"notifyEnd":new UtcDate(1460357844353),"notifyText":null,"f_username":null,"isonline":0,"IfSendAlarmEmail":false,"AlarmEmail":null},{"carID":100111,"teamID":0,"carNO":"carNO2 TEst","simNO":"23","machineNO":"13000000005","controlPassword":null,"machineType":null,"protocol":7,"routeway":0,"carType":null,"carBrand":null,"carColor":null,"installPlace":"7","installPerson":null,"businessPerson":null,"joinTime":new UtcDate(1460358033120),"overServiceTime":new UtcDate(1491840000000),"carRemark":null,"driver":null,"driverTel":null,"driverMobile":null,"driver2":null,"driver2Tel":null,"driver2Mobile":null,"password":null,"driverAddress":null,"driverFax":null,"driverCompany":null,"buyTime":null,"stoped":0,"specialRequest":"0","driverRemark":null,"regionAlarm":0,"regionID":0,"positionID":0,"notify":0,"notifyStart":new UtcDate(1460358033120),"notifyEnd":new UtcDate(1460358033120),"notifyText":null,"f_username":null,"isonline":0,"IfSendAlarmEmail":false,"AlarmEmail":null}]}    

I have tested on an online jsonparse http://json.parser.online.fr/

It is also returning the same error.

If I am not mistaken, the date is in a wrong format but I am not allowed to touch the server side. What have I done wrong?

Upvotes: 0

Views: 46

Answers (1)

blex
blex

Reputation: 25634

The dates are obviously not valid JSON. But if you don't have access to the server, you could convert these dates to regular timestamps by using Regex:

// Will convert   new UtcDate(1460357844353)      to      1460357844353
var data = JSON.parse( result.replace(/new UtcDate\(([0-9]+)\)/gi, "$1") );

JS Fiddle demo

Upvotes: 1

Related Questions