JSON复杂格式

时间:2015-07-20 07:17:56

标签: php mysql json

首先,我必须承认我在处理JSON时非常糟糕。我需要在一个困扰我大约两周的问题上寻求帮助。所以,我正在尝试为iOS / Android应用程序创建一个基本的Web服务。我按照App开发人员的说明正确创建API。一切似乎都很容易,直到我达到创建getContents.php的程度。 这是我用于获取和回显数据的PHP代码:

FileOutputStream out;
            try {
                out = new FileOutputStream(new File("C:\\work\\abc.xlsx"));

                workbook.write(out);  
                out.close(); 
                System.out.println("abc.xlsx written successfully on disk.");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

我从该代码得到的是:

<?php
header('content-type:application/json;');
$article_id = $_GET["ArticleID"];


$link = mysql_connect('localhost','bridgeapp_user','') or die('Cannot connect to the DB');
mysql_select_db('bridgeapp',$link) or die('Cannot select the DB');  


mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'", $link);


if (isset($article_id)) {
    $query = "SELECT description AS `content`, 'http://offercat.com/media/com_jbusinessdirectory/pictures'+logoLocation AS `images` FROM vrfmp_jbusinessdirectory_companies WHERE id = '".$article_id. "'";
    $result = mysql_query($query,$link) or die('Errant query:  '.$query);

}

$rows = array();
while($r = mysql_fetch_assoc($result)) {
    $rows[] = $r;
//    $rows = str_replace("name", "title", "$rows");
}

echo json_encode($rows);

@mysql_close($link);

我想得到的东西看起来像这样:

[{"content":"<p>asdasdasd<\/p>","images":"0"}]

关于如何实现这一目标的任何想法? 提前谢谢!

1 个答案:

答案 0 :(得分:3)

$rows = array();
while($r = mysql_fetch_assoc($result)) {

$images[]=$r['images'];


}
$social="some content here";

$content="some content here ";

$rows=array("content"=>$content,"social_link"=>$social,"images"=>$images);

echo json_encode($rows);