簡體   English   中英

在具有多個對象的字符串上使用eval

[英]use eval on string with multiple objects

如何將以下字符串評估為多個對象? 目前它只是做第一個然后忽略其余的,忽略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
                        }

                    }

                }

              }

我會避免做eval - JSON.parse()更安全。 由於看起來對象已經以逗號分隔,因此您應該能夠將其轉換為數組並對其進行解析。 例如,

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

其中objectString具有您上面粘貼的值。 objectArray現在是一個包含每個對象的數組。

為此,使用JSON.parse會不會更容易?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM