你好我需要在php中写一个json文件,就像这样。我遵循了几个教程,但我不明白如何正确地放置括号:
{"data": [
{
"album": "OK Computer",
"artist": "Radiohead",
"first": true,
"id": "okcomputer",
"image": "okcomputer.png",
"tracklist": [
"Airbag",
"Paranoid Android",
"Subterranean Homesick Alien",
"Exit Music (For a Film)",
"Let Down",
"Karma Police",
"Fitter Happier",
"Electioneering",
"Climbing Up the Walls",
"No Surprises",
"Lucky",
"The Tourist"
],
"url": "https://itunes.apple.com/us/album/ok-computer/id696736813?i=696737042&uo=4&at=1l3v7Hz",
"year": "1997"
}
]}
怎么可能?谢谢!
答案 0 :(得分:2)
如果您将数据定义为普通的PHP数组,那么您可以使用json_encode函数为您执行此操作。见http://php.net/manual/en/function.json-encode.php
例如:
constexpr
答案 1 :(得分:0)
如果你想在JSON中存储一些PHP数据,你必须把它放到一个数组中,然后像你的情况一样使用json_encode函数
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerId>groovy-eclipse-compiler</compilerId>
<verbose>true</verbose>
<fork>true</fork>
<compilerArguments>
<javaAgentClass>lombok.launch.Agent</javaAgentClass>
</compilerArguments>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-compiler</artifactId>
<version>2.9.2-01</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-batch</artifactId>
<version>2.4.3-01</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.8</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</pluginManagement>
答案 2 :(得分:0)
尝试这个
<?php
$output=array();
$data=array();
$item=array();
$item['album']="OK Computer";
$item['other']="same as album";
$tracklist=array('a','b','c');
$item['tracklist']=$tracklist;
array_push($data,$item);
$output['data']=$data;
echo json_encode($output);
?>
输出:
{
"data": [{
"album": "OK Computer",
"other": "same as album",
"tracklist": ["a", "b", "c"]
}]
}