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

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);
}

I guess your $jsonData is not actually JSON. Try using a json helper

$jsonData = Mage::helper('core')->jsonEncode($data)