此问题与提及的重复答案/问题完全无关......
我有一个包含JavaScript的超过1,000个项目的大型列表我这种格式...
var plugins = [
{
name: "Roundabout - Interactive, turntable-like areas",
url: "http:\/\/fredhq.com/projects/roundabout/",
tag: "slide"
},
{
name: "Slides - Simple slideshow plugin for jQuery",
url: "http://slidesjs.com/",
tag: "slide"
}
......lots more
]
我的目标是将它们导入到像这样的PHP数组中....
Array
(
[0] => Array
(
[name] => Roundabout - Interactive, turntable-like areas/
[url] => http://fredhq.com/projects/roundabout/
[tag] => slide
)
[1] => Array
(
[name] => Slides - Simple slideshow plugin for jQuery
[url] => http://slidesjs.com/
[tag] => slide
)
[2] => Array
(
[name] => Orbit - jQuery Image Slider Plugin
[url] => http://www.zurb.com/playground/jquery_image_slider_plugin/
[tag] => slide
)
)
我尝试使用PHP的json_decode()
函数,但源JS没有包含在"
中的密钥,所以它不起作用。
当我在键和一些反斜杠周围手动添加"
时,PHP json_decode()
正常工作....
$json = '[{
"name": "Roundabout - Interactive, turntable-like areas/",
"url": "http:\/\/fredhq.com/projects/roundabout/",
"tag": "slide"
},
{
"name": "Slides - Simple slideshow plugin for jQuery",
"url": "http:\/\/slidesjs.com\/",
"tag": "slide"
},
{
"name": "Orbit - jQuery Image Slider Plugin",
"url": "http:\/\/www.zurb.com/playground/jquery_image_slider_plugin/",
"tag": "slide"
}]';
echo '<pre>';
print_r(json_decode($json, true));
echo '</pre>';
问题是我的大型列表没有用引号括起来的键,所以我需要一种方法来自动化它。
有人帮助我实现最终目标,因为我的尝试到目前为止都失败了吗?
答案 0 :(得分:0)
看起来问题可以通过采用这种方法手动解决,因为已经说过这里介绍的JSON是有效的,最好使用控制台或其他东西来使JSON对所有地方都有效,不仅仅是在JavaScript中使用:
plugins = JSON.stringify(plugins);
这会使plugins
看起来像:
"[{"name":"Roundabout - Interactive, turntable-like areas","url":"http://fredhq.com/projects/roundabout/","tag":"slide"},{"name":"Slides - Simple slideshow plugin for jQuery","url":"http://slidesjs.com/","tag":"slide"}]"
删除引号并将其放入JSONLint将提供有效的JSON,如下所示:
[
{
"name": "Roundabout - Interactive, turntable-like areas",
"url": "http://fredhq.com/projects/roundabout/",
"tag": "slide"
},
{
"name": "Slides - Simple slideshow plugin for jQuery",
"url": "http://slidesjs.com/",
"tag": "slide"
}
]
现在最好的方法是复制此输出并将其保存在文本中。