JSON.parse syntax issue with a string in array structure

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 ?

Answers


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


Need Your Help

Display nested items in JSON object with jQuery

jquery json

I'm trying to display nested items in my JSON object. Right now their value is [object Object]. I'm using a $.each loop but it doesn't seem to be getting the values of the nested items. What am I d...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.