1

I have a simple string which is structured like this:

[[Home],[685,300],[800,380],[685,300],[Home]]

Basically its an array of co-ordinates, to create a path. The Home is used because this changes based on user's location on the map.

The problem i face is when i pass it from PHP to JS.

I json_encode the data and pass it across like this:

[{"path":"[[Home],[685,300],[800,380],[685,300],[Home]]","id":"1"}]

Then i create my object for it after i JSON.parse:

paths = {};
for(var i in data){     
  paths[data[i].id] = {}
  paths[data[i].id].path = data[i].path;
}

The problem is data[i].path is still just a string and not a useable array. So i tried to add a secondary JSON.parse to change the string to a useable array like this:

paths[data[i].id].path = JSON.parse(data[i].path);

But this causes:

 Unexpected token H 

The H is obviously coming from Home that i put in the array, so I am wondering what i can do convert it to a useable array ?

1 Answer 1

2

Your string is not valid JSON. Home would be a javascript identifier of that name. "Home" is a string. The quotes are missing.

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

1 Comment

Ah damn ! I tried single quotes but didnt try double quotes xD Thanks @Jan!

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.