简体   繁体   English

在具有多个对象的字符串上使用eval

[英]use eval on string with multiple objects

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. 目前它只是做第一个然后忽略其余的,忽略PHP,这是通过PHP生成的,这是我需要评估它的原因,以便我可以使用它作为jQuery插件的选项。

            {
                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
                        }

                    }

                }

              }

I would avoid doing eval - JSON.parse() is a lot safer. 我会避免做eval - JSON.parse()更安全。 Since it looks like the objects are already comma separated, you should be able to just turn it into an array and parse that. 由于看起来对象已经以逗号分隔,因此您应该能够将其转换为数组并对其进行解析。 Eg, 例如,

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

Where objectString has the value of what you pasted above. 其中objectString具有您上面粘贴的值。 objectArray will now be an array with each of your objects in it. objectArray现在是一个包含每个对象的数组。

为此,使用JSON.parse会不会更容易?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM