如何在CakePHP中格式化此JSON的输出?

时间:2013-03-03 17:25:11

标签: php json cakephp

我正在使用CakePHP将JSONS发送到我的Android应用程序。我目前在我的模型中有这个功能,它让我在某个地方的某个半径范围内的最近位置:

public function getNearest($lat = 54.980503, $lng = -1.614349, $radius = 25, $max = 30)
{
    $query = "SELECT *, 
             ( 3959 * acos( cos( radians('$lat') ) * cos(radians(`lat`)) 
             * cos(radians(`long`) - radians('$lng') ) 
             + sin( radians('$lat') ) * sin( radians(`lat`) ) ) ) 
             AS distance FROM locations HAVING distance < '$radius' ORDER BY distance LIMIT 0 , {$max}";
    return $this->query($query);        
}

这是我的控制器:

class LocationsController extends AppController
{
    public $helpers = array('Html', 'Form');
    public $components = array('RequestHandler');
    public $viewClass = 'Json'; 
    function index()
    {
        $locations = $this->Location->getNearest(); 
        $this->set(compact('locations'));
        $this->set('_serialize', array('locations'));
    }
}

这是它以正常数组格式给出的输出:

array(2) 
{ 
    [0]=> array(2) 
    { 
        ["locations"]=> array(9) 
        { 
            ["id"]=> string(1) "1" 
            ["name"]=> string(20) "Newcastle University" 
            ["terrain"]=> string(1) "5" 
            ["difficulty"]=> string(1) "2"
            ["ratings"]=> string(1) "4" 
            ["uid"]=> string(1) "2" 
            ["long"]=> string(8) "-1.61624" 
            ["comid"]=> NULL 
            ["lat"]=> string(7) "54.9787" 
        } 
    [0]=> array(1) 
    { 
        ["distance"]=> string(19) "0.14548355269404845" 
    } 
 } 
 [1]=> array(2) 
 { 
    ["locations"]=> array(9) 
    {
        ["id"]=> string(1) "2" 
        ["name"]=> string(10) "Vale House" 
        ["terrain"]=> string(1) "4" 
        ["difficulty"]=> string(1) "3" 
        ["ratings"]=> string(1) "4"
        ["uid"]=> string(1) "2" 
        ["long"]=> string(8) "-1.58939" 
        ["comid"]=> NULL 
        ["lat"]=> string(7) "54.9849" 
    } 
    [0]=> array(1) 
    { 
        ["distance"]=> string(18) "1.0352356381517442" } 
    }
}

如果我打开CakePHP中的JSONView类,我会得到:

{
   "locations":[
      {
         "locations":{
            "id":"1",
            "name":"Newcastle University",
            "terrain":"5",
            "difficulty":"2",
            "ratings":"4",
            "uid":"2",
            "long":"-1.61624",
            "comid":null,
            "lat":"54.9787"
         },
         "0":{
            "distance":"0.14548355269404845"
         }
      },
      {
         "locations":{
            "id":"2",
            "name":"Vale House",
            "terrain":"4",
            "difficulty":"3",
            "ratings":"4",
            "uid":"2",
            "long":"-1.58939",
            "comid":null,
            "lat":"54.9849"
         },
         "0":{
            "distance":"1.0352356381517442"
         }
      }
   ]
}

正如你所看到的,那里有很多不必要的字符串,例如,为什么有3个位置?为什么距离分开?

如果输出可以是以下几行,那将是很好的。

{
    "locations":
    [{
            "id":"1",
            "name":"Newcastle University",
            "terrain":"5",
            "difficulty":"2",
            "ratings":"4",
            "uid":"2",
            "long":"-1.61624",
            "comid":null,   
            "lat":"54.9787"
            "distance":"0.14548355269404845"
    },
    {
            "id":"2",
            "name":"Vale House",
            "terrain":"4",
            "difficulty":"3",
            "ratings":"4",
            "uid":"2",
            "long":"-1.58939",
            "comid":null,
            "lat":"54.9849"
            "distance":"1.0352356381517442"
    }

   ]
}

1 个答案:

答案 0 :(得分:2)

正如@j0k所提到的,在SQL语句中使用*是“从数据库表中选择所有列”。由于“距离”是计算字段,因此不是特定表格的“部分”,因此不会以“位置”作为前缀。 CakePHP将在结果集中的单独“键”中包含这些字段。但是,重写该结果数组的结构应该不难。

为什么有3个地点? 因为您要求数据库使用您的查询在<25>半径' *内返回'最多30个位置(尽管我在示例中只看到 2

注意 您正在使用自定义查询但不会转义/清理您的值。使用自定义查询时,CakePHP不会为您执行此操作,允许SQL注入。请小心处理!请注意,CakePHP 支持预准备语句(http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#prepared-statements

最好的选择是将查询重写为标准的CakePHP find()。您可能需要创建一些虚拟字段才能使其正常工作; http://book.cakephp.org/2.0/en/models/virtual-fields.html

我现在不在我的电脑上,但如果你需要更多的例子,我可以写一些东西