从存储在MySql中的数据创建json API请求响应

时间:2012-12-18 12:48:41

标签: php mysql json codeigniter getjson

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

    **]**

     }
     }
    ';

1 个答案:

答案 0 :(得分:1)

建立array。完成后,只需使用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}
?>