-1

How would I return the following string as an object

{src:'img/testimage.jpg', coord : {x:17, y:39}, width:200, height, 200} 

update

I have a php file that outputs JSON. I've used AJAX to to access the JSON in my js.

I've used JSON.parse(json_string) so I now have my object. This what is returned:

[{"name":"img","attributes":"{src:'img\/testimage.jpg', coord : {x:17, y:39}, width:200, height, 200}","comments":"image element with attributes"},{"name":"triangle","attributes":"{bgColor : '#FF0000', coord : {x:500, y:300}, width:50, height, 50}","comments":"triangle"}] 

I can now use a for loop to go through the bits.

for(key in json_object) {
 var name_type = json_object[key].name;
 var attrib = json_object[key].attributes;
}

here attrib returns

{src:'img/testimage.jpg', coord : {x:17, y:39}, width:200, height, 200}. 

It is this string that I need to convert into an object.

Thanks Dave

magicspon
  • 926
  • 2
  • 9
  • 28

4 Answers4

1

You have to use JSON.parse(string) but first you need to make your string a valid JSON. which means you have to do the following:

instead of {src:'img/testimage.jpg'... it should be {"src":"img/testimage.jpg"... (note the quotes on the src). Each key should be wrapped inside quotes "" or '', and each value should be exactly how it would be represented as code, for example: a String would be wrapped in quotes, an int won't;

So your final String would be:

EDIT: as War10ck says, the string should be escaped.

"{\"src\":\"img/testimage.jpg\", \"coord\":{\"x\":17, \"y\":39}, \"width\":200, \"height\":200}"
Christopher Francisco
  • 15,672
  • 28
  • 94
  • 206
  • This is still not right. See my [answer](http://stackoverflow.com/questions/20481895/turn-string-into-an-object-with-javascript/#answer-20482158). While you are using double quotes as the spec requires, the internal double quotes (i.e. nested strings) are not properly escaped. – War10ck Dec 09 '13 at 22:29
  • 1
    The thing is, if your `String` was formatted properly, when you go `var attrib = json_object[key].attributes;` it should return an object. If what you're getting is an `String` it's because it is not formatted properly – Christopher Francisco Dec 09 '13 at 22:29
  • I see. The data is coming from a mysql database. The attributes value in the database is {src:'img/testimage.jpg',and:'so on'}. So the problem is with string in the database. Thanks guys – magicspon Dec 09 '13 at 22:33
1

As many have pointed out, you should really store this data as valid JSON in the first place and as another post has pointed out your object isn't even a valid javascript object-literal. How you choose to serialise your data is ultimately your choice but you must at least stick to some rules when encoding it, otherwise how can you expect to parse it?

... width:200, height, 200}
                     ^------- Invalid JS Object Notation

That said, if for whatever reason you can't easily change your setup (you should aspire to refactor eventually) you can have javascript evaluate the expression as long as you trust the source.

var str, obj;
str = "{src:'img/testimage.jpg', coord:{x:17, y:39}, width:200, height:200}";
obj = new Function('return '+str)();

Related Reading:

Why is using the JavaScript eval function a bad idea?

Community
  • 1
  • 1
Emissary
  • 9,954
  • 8
  • 54
  • 65
0

Your JSON is not formatted correctly. Should be:

{src:'img/testimage.jpg', coord : {x:17, y:39}, width:200, height: 200} 

(Note the COLON after height, not a COMMA)

jraede
  • 6,846
  • 5
  • 29
  • 32
  • Your JSON is not formatted correctly either. According to the JSON spec strings should be enclosed in double quotes `"`. – War10ck Dec 09 '13 at 22:23
  • Hello, I've updated my question. Initially I have a nice valid JSON object. One of the objects inside my object is the string I have mentioned – magicspon Dec 09 '13 at 22:25
  • If you copied/pasted from your code, you do NOT have a valid (or "parsable", since War10ck is technically correct) JSON object. – jraede Dec 09 '13 at 22:26
0

As the other answers point out, you need to have a properly formatted properly escaped JSON string to use JSON.parse(). Try this:

"{\"src\":\"img/testimage.jpg\", \"coord\":{\"x\":17, \"y\":39}, \"width\":200, \"height\":200}"

According to the JSON Spec, all strings must be enclosed in double quotes ". All strings within a JSON string should have properly escaped double quotes \".

War10ck
  • 12,387
  • 7
  • 41
  • 54