Magento 1.7 – Convert Magento’s Returned Object to JSON Properly

jsonmagento-1.7object

I made a customer log-in to the site programmatically… I am properly returned the session values in form of an object.

Now i want this data carried over and returned from the function in JSON format.

I know magento has its own encoding and decoding functions… But this conversion does not some how take place properly.

I mean session object contains customer's email, first, last name,etc. But these are not reflected in converted json… So how do I convert this properly??

Thanks in advance

Here is my code..

<?php

function loginUser( $email, $password )
{
    require_once ("../../app/Mage.php");
    umask(0);
    ob_start();
    session_start();
    Mage::app('default');
    Mage::getSingleton("core/session", array("name" => "frontend"));

    $websiteId = Mage::app()->getWebsite()->getId();
    $store = Mage::app()->getStore();
    $customer = Mage::getModel("customer/customer");
    $customer->website_id = $websiteId;
    $customer->setStore($store);
    try {
        $customer->loadByEmail($email);
        $session = Mage::getSingleton('customer/session');
        $session->login($email, $password);
        $session->setCustomerAsLoggedIn($customer);
        $jsonData = Mage::helper('core')->jsonEncode($session);
        var_dump($jsonData);

    }catch(Exception $e){
        return $e->getMessage();
    }


  } 

    $test = loginUser("test1.igex@gmail.com","testigex");
    //var_dump($test);
?>

Best Answer

It is not good idea to return a php object to json value.As per as,php concept json is convert a string varible and an array of string variables to json object.

Json does not capable to convert a php to json object.

If you want data then need to return customer data in array format.

just like:

$jsonData = Mage::helper('core')->jsonEncode($customer->getData());

getData() is give Customer data in array format.

Related Topic