1

How can I evaluate the following string into multiple objects? at the moment it just does the first then ignores the rest, Ignore the PHP, this is generated via PHP, that being the reason I need to eval it so that I can use it as the options for a jQuery plugin.

            {
                latLng:['.$result->lat.','.$result->lon.'], 
                options:
                {
                    shadow: 
                    {

                        url: "'.BASE_URL.'css/png/markerBg.png",
                        scaledSize: 
                        {
                            width:40,
                            height:43.5
                        }
                    },
                    icon: 
                    {
                        url: "'.$result->user->profileImage.'",
                        scaledSize: 
                        {
                            width:32,
                            height:32
                        },
                        anchor: 
                        {
                            x: 16,
                            y: 40
                        }

                    }

                }

              },
            {
                latLng:['.$result->lat.','.$result->lon.'], 
                options:
                {
                    shadow: 
                    {

                        url: "'.BASE_URL.'css/png/markerBg.png",
                        scaledSize: 
                        {
                            width:40,
                            height:43.5
                        }
                    },
                    icon: 
                    {
                        url: "'.$result->user->profileImage.'",
                        scaledSize: 
                        {
                            width:32,
                            height:32
                        },
                        anchor: 
                        {
                            x: 16,
                            y: 40
                        }

                    }

                }

              },
            {
                latLng:['.$result->lat.','.$result->lon.'], 
                options:
                {
                    shadow: 
                    {

                        url: "'.BASE_URL.'css/png/markerBg.png",
                        scaledSize: 
                        {
                            width:40,
                            height:43.5
                        }
                    },
                    icon: 
                    {
                        url: "'.$result->user->profileImage.'",
                        scaledSize: 
                        {
                            width:32,
                            height:32
                        },
                        anchor: 
                        {
                            x: 16,
                            y: 40
                        }

                    }

                }

              }
2
  • 1
    you could try putting the string inside [ ] then the result should be an array of objects Commented Mar 28, 2013 at 15:45
  • If you have to use Eval then you know your in trouble. Commented Mar 28, 2013 at 15:52

2 Answers 2

3

I would avoid doing eval - JSON.parse() is a lot safer. Since it looks like the objects are already comma separated, you should be able to just turn it into an array and parse that. E.g.,

var objectArray = JSON.parse('[' + objectString + ']');

Where objectString has the value of what you pasted above. objectArray will now be an array with each of your objects in it.

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

6 Comments

Yes it would be and I tried using JSON.parse() first however, I kept getting the following error: SyntaxError: JSON Parse error: Expected '}' and tried everything to solve it but nothing....
Hmm... on second thought, that's not really valid JSON, just comma-separated object literals. Do you have control over the PHP code? This format is not at all browser friendly.
that format is what is needed as thats the format required for the jQuery plugin.... this isn't PHP this is Javascript.
The jQuery plugin won't care as long as you end up with a valid object. Right now what you're generating back in PHP-land is close to being an array of objects, but it's really just some comma-separated object-literal syntax. This is a problem that you can (and should) solve without the use of eval. Ideally this is something you would solve in your PHP code where you're generating this output. If that's absolutely not an option, you can solve this less elegantly client-side, but I think I'd need to see more of your client-side code where you're using the above output.
what is being generated is what is required by the jquery plugin. see here: gmap3.net/en/catalog/10-overlays/overlay-37, i am sending this from php as it has to be dynamic pointers from the db, but the format of it is pulled from the plugin.
|
0

wouldn't it be easier to use JSON.parse for this?

Comments

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.