如何在cakephp中重命名json数组名?

时间:2017-10-08 16:43:38

标签: cakephp cakephp-3.0

我想更改以下代码的json名称

例如

QUrl url("https://www.cyclestreets.net/api/journey.json");
QMap<QString, QString> queryStrings;
queryStrings["key"] = "cdccf13997d59e70";
queryStrings["reporterrors"] = QLatin1Char('1');
queryStrings["plan"] = settings[QStringLiteral("plan")].toString();
if (queryStrings["plan"].isEmpty()) {
    queryStrings["plan"] = QStringLiteral("balanced");
}
queryStrings["speed"] = settings[QStringLiteral("speed")].toString();
if (queryStrings["speed"].isEmpty()) {
    queryStrings["speed"] = QStringLiteral("20");
}
GeoDataCoordinates::Unit const degree = GeoDataCoordinates::Degree;
QString itinerarypoints;
itinerarypoints.append(QString::number(route->source().longitude(degree), 'f', 6) + QLatin1Char(',') + QString::number(route->source().latitude(degree), 'f', 6));
for ( int i=1; i<route->size(); ++i ) {
    itinerarypoints.append(QLatin1Char('|') +  QString::number(route->at(i).longitude(degree), 'f', 6) + QLatin1Char(',') + QString::number(route->at(i).latitude(degree), 'f', 6));
}
queryStrings["itinerarypoints"] = itinerarypoints;

QUrlQuery urlQuery;
for( const QString& key: queryStrings.keys()){
    urlQuery.addQueryItem(key, queryStrings.value(key));
}
url.setQuery( urlQuery);

m_request.setUrl( url );
m_request.setRawHeader( "User-Agent", HttpDownloadManager::userAgent( "Browser", "CycleStreetsRunner" ) );
QNetworkReply *reply = m_networkAccessManager.get( m_request );
connect( reply, SIGNAL(error(QNetworkReply::NetworkError)),
         this, SLOT(handleError(QNetworkReply::NetworkError)), Qt::DirectConnection );

void CycleStreetsRunner::handleError( QNetworkReply::NetworkError error )
{
    qDebug() << "Error: " << error;
}

将代码更改为

     {
        "id": 1,
        "mb_item_list": {
            "item_name": "Something"
        }
    }

这里我分享了控制器代码

{
        "id": 1,
        "name": "Something"

    }

1 个答案:

答案 0 :(得分:1)

我认为你可以在查找器选项中使用fieldsFields允许我们为数据库字段生成别名,并帮助避免嵌套数组:

    $searchitem = $this->ModelA->find('all' , [
            'fields' => [
                'id', 
                'name' => 't1.whatever'
            ], 
            'join' => [
                'table' => 'model_b', 
                'alias' => 't1', 
                'type' => 'INNER', 
                'conditions' => [
                    't1.id = ModelA.id'
                ]
            ]
        ])
    ->toArray();

使用此方法生成的示例输出:

[  
   {  
      "id":1,
      "name":"Config"
   },
   {  
      "id":2,
      "name":"Dashboard"
   },
   {  
      "id":3,
      "name":"Files"
   }
]