0

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?

7
  • seems like this is a problem on your client side, I can parse that in the mentioned parser without any errors. Commented Apr 12, 2016 at 14:23
  • Are you use you are not trying to parse an object? console.log(typeof result); Commented Apr 12, 2016 at 14:23
  • Parses fine for me, both in the online parser and in a test harness I just created. Commented Apr 12, 2016 at 14:25
  • Sorry guys, I pasted the wrong string Commented Apr 12, 2016 at 14:25
  • 2
    ""joinTime":new UtcDate(1460357844353)" - not valid JSON. So, the server is wrong here. Commented Apr 12, 2016 at 14:29

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

This is just smart. Thank you blex!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.