dongpang2483 2013-10-22 07:39
浏览 189
已采纳

REST API以JSON格式的响应

In magento as we use the rest url to access the data, as http://localhost/magento/api/rest/products it returns in xml format instead of that I need JSON.

I have tried below code, but no use

$this->getResponse()->setHeader('Content-type', 'application/json');
$this->getResponse()->setBody($jsonData);

in the folder \magento\app\code\core\Mage\Api\Controller\Action.php

  • 写回答

2条回答 默认 最新

  • dsubq24666 2013-10-22 08:03
    关注

    vinox, you should override the default file Request.php. copy \app\code\core\Mage\Api2\Model\Request.php to your local directory and add the following code just before end of the getAcceptTypes() Method.

    unset($orderedTypes);
    $orderedTypes=Array("application/json" => 1);
    

    in other way your getAcceptTypes() method should look like this.

    public function getAcceptTypes(){
    $qualityToTypes = array();
    $orderedTypes   = array();
    
    foreach (preg_split('/,\s*/', $this->getHeader('Accept')) as $definition) {
        $typeWithQ = explode(';', $definition);
        $mimeType  = trim(array_shift($typeWithQ));
    
        // check MIME type validity
        if (!preg_match('~^([0-9a-z*+\-]+)(?:/([0-9a-z*+\-\.]+))?$~i', $mimeType)) {
            continue;
        }
        $quality = '1.0'; // default value for quality
    
        if ($typeWithQ) {
            $qAndValue = explode('=', $typeWithQ[0]);
    
            if (2 == count($qAndValue)) {
                $quality = $qAndValue[1];
            }
        }
        $qualityToTypes[$quality][$mimeType] = true;
    }
    krsort($qualityToTypes);
    
    foreach ($qualityToTypes as $typeList) {
        $orderedTypes += $typeList;
    }
    unset($orderedTypes);
    $orderedTypes=Array("application/json" => 1);
    return array_keys($orderedTypes);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有偿求苍穹外卖环境配置
  • ¥15 代码在keil5里变成了这样怎么办啊,文件图像也变了,
  • ¥20 Ue4.26打包win64bit报错,如何解决?(语言-c++)
  • ¥15 clousx6整点报时指令怎么写
  • ¥30 远程帮我安装软件及库文件
  • ¥15 关于#自动化#的问题:如何通过电脑控制多相机同步拍照或摄影(相机或者摄影模组数量大于60),并将所有采集的照片或视频以一定编码规则存放至规定电脑文件夹内
  • ¥20 深信服vpn-2050这台设备如何配置才能成功联网?
  • ¥15 Arduino的wifi连接,如何关闭低功耗模式?
  • ¥15 Android studio 无法定位adb是什么问题?
  • ¥15 C#连接不上服务器,