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 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器