0

我在我的 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"
        }

    **]**

     }
     }
    ';
4

1 回答 1

1

构建一个数组。完成后,只需使用json_encode ($array) 以正确有效的 json 格式输出。

这样做将保护您的代码,您不会得到意想不到的结果 - 因为与 json 格式的代码相比,处理数组要容易得多。

代码示例

<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);

echo json_encode($arr);

//Output: {"a":1,"b":2,"c":3,"d":4,"e":5}
?>
于 2012-12-18T12:53:08.953 回答