我在我的 Codeigniter 网站中开发了一个函数,通过 API 调用请求数据以检索存储在 mysql 表中的时间线数据。我的 json 响应没有正确输出。
我需要创建一个对 $.getJSON 的 json 响应,我是在 php 中完成的。下面是我为从 mysql 获取数据并创建 json 响应而编写的代码。
public function index_get() {
$rs = mysql_query("SELECT headline, type, text, media, credit, caption FROM media");
$timeline = '
{
"@headline": "' . $row[ 'headline' ] . '",
"@type": "' . $row[ 'type' ] . '",
"@text": "' . $row[ 'text' ] . '",
"asset": {
"@media": "' . $row[ 'type' ] . '",
"@credit": "' . $row[ 'type' ] . '",
"@caption": "' . $row[ 'type' ] . '",
';
$newsdate = mysql_query("SELECT startDate, endDate, headline, text, tag, media, thumbnail, credit, caption FROM news");
while( $row = mysql_fetch_array( $newsdate ) ){
$newsitem[] = array(
'startDate'=> $row[ 'startDate' ],
'endDate' => $row[ 'endDate' ],
'headline' => $row[ 'headline' ],
'text' => $row[ 'text' ],
'tag' => $row[ 'tag' ],
'asset' => array(
'media' => $row[ 'media' ],
'thumbnail' => $row[ 'thumbnail' ],
'credit' => $row[ 'credit' ],
'caption' => $row[ 'caption' ]
));
$row1 = mysql_query("SELECT startDate, endDate, headline, tag FROM era");
$era = '
"era": {
"@startDate": "' . $row1[ 'startDate' ] . '",
"@endDate": "' . $row1[ 'endDate' ] . '",
"@headline": "' . $row1[ 'headline' ] . '",
"@tag": "' . $row1[ 'tag' ] . '"
';
$row2 = mysql_query("SELECT startDate, endDate, headline, value FROM chart");
$chart = '
"chart": {
"@startDate": "' . $row2[ 'startDate' ] . '",
"@endDate": "' . $row2[ 'endDate' ] . '",
"@headline": "' . $row2[ 'headline' ] . '",
"@value": "' . $row2[ 'value' ] . '"
}
}
}
}';
$this->response(array(
'timeline' =>
$newsitem,
'date' => $date,
'era' => $era,
'chart' => $chart ), 200);
}
我喜欢的结果如下所示,我遇到的问题是我的 json 响应中没有得到所有结束的 { 和 [,请参见下面的粗体代码。从存储在mysql中的数据中,在json中执行如下时间线的正确方法是什么?
$jsonresponse = '
{ "timeline":
{
"headline":"The Main Timeline Headline Goes here",
"type":"default",
"text":"<p>Intro body text goes here, some HTML is ok</p>",
"asset": {
"media":"http://yourdomain_or_socialmedialink_goes_here.jpg",
"credit":"Credit Name Goes Here",
"caption":"Caption text goes here"
},
"date": **[**
{
"startDate":"2011,12,10",
"endDate":"2011,12,11",
"headline":"Headline Goes Here",
"text":"<p>Body text goes here, some HTML is OK</p>",
"tag":"This is Optional",
"asset": {
"media":"http://twitter.com",
"thumbnail":"optional-32x32px.jpg",
"credit":"Credit Name Goes Here",
"caption":"Caption text goes here"
}
{,
{
"startDate":"2012,1,26",
"endDate":"2012,1,27",
"headline":"Sh*t Politicians Say",
"text":"<p>In true political fashion",
"asset": {
"media":"http://youtu.be/u4XpeU9erbg",
"credit":"",
"caption":""
}
},
{
"startDate":"2012,1,10",
"headline":"Sh*t Nobody Says",
"text":"<p>Have you ever heard someone say</p>",
"asset": {
"media":"http://youtu.be/f-x8t0JOnVw",
"credit":"",
"caption":""
}
},
**]**,
"era": **[**
{
"startDate":"2011,12,10",
"endDate":"2011,12,11",
"headline":"Headline Goes Here",
"tag":"This is Optional"
}
**]**,
"chart": **[**
{
"startDate":"2011,12,10",
"endDate":"2011,12,11",
"headline":"Headline Goes Here",
"value":"28"
}
**]**
}
}
';