0

This is my first post so please let me know if I missed sharing any detail on my problem.

I have below files ready in Magento kept in app/code/community:

A. Autogen > Restapi > etc > api2.xml

B. Autogen > Restapi > etc > config.xml

C. Autogen > Restapi > Model > Api2 > Order.php

D. Autogen > Restapi > Model > Api2 > Order > Rest > Admin > V1.php

Below is the part of code written in V1.php:

protected function _retrieveCollection() {
    $return_variable = array();
    $page = $this->getRequest()->getParam('page');
    $limit = $this->getRequest()->getParam('limit');

    $collection = Mage::getModel('sales/order')->getCollection()
                                                ->setCurPage($page)
                                                ->setPageSize($limit);

    $return_variable['order_count'] = $collection->getSize();
    $order_counter = 0;
    foreach ($collection as $order) {
        $return_variable['order'][$order_counter]['order_id'] = $order->getId();
        $return_variable['order'][$order_counter]['increment_id'] = $order->getIncrementId();

        $order_counter = $order_counter + 1;
    }

    return json_encode($return_variable);   //I will refer this line again
}

When I hit restapi with Restclient, I get below output which is not fit for json_decode:

"{\"order_count\":2050,\"order\":[{\"order_id\":\"2282\",\"increment_id\":\"100003584\"},{\"order_id\":\"2281\",\"increment_id\":\"100003583\"},{\"order_id\":\"2280\",\"increment_id\":\"100003582\"},{\"order_id\":\"2279\",\"increment_id\":\"100003581\"},{\"order_id\":\"2278\",\"increment_id\":\"100003579\"},{\"order_id\":\"2277\",\"increment_id\":\"100003578\"},{\"order_id\":\"2276\",\"increment_id\":\"100003577\"},{\"order_id\":\"2275\",\"increment_id\":\"100003576\"},{\"order_id\":\"2274\",\"increment_id\":\"100003575\"},{\"order_id\":\"2273\",\"increment_id\":\"100003574\"},{\"order_id\":\"2272\",\"increment_id\":\"100003573\"},{\"order_id\":\"2271\",\"increment_id\":\"100003572\"},{\"order_id\":\"2270\",\"increment_id\":\"100003571\"},{\"order_id\":\"2269\",\"increment_id\":\"100003570\"},{\"order_id\":\"2268\",\"increment_id\":\"100003569\"},{\"order_id\":\"2267\",\"increment_id\":\"100003568\"},{\"order_id\":\"2265\",\"increment_id\":\"100003565\"},{\"order_id\":\"2264\",\"increment_id\":\"100003564\"},{\"order_id\":\"2263\",\"increment_id\":\"100003563\"},{\"order_id\":\"2262\",\"increment_id\":\"100003562\"}]}"

When I change (See commented line in above code) from "return json_encode($return_variable);" to "return $return_variable;", I get below output. This response is JSON fit for decode but I should receive more key-value pair instead of just 1:

{"order_count":2050,"order":[{"order_id":"2282","increment_id":"100003584"}]}

Please help me to get JSON fit for decode data with all key-value pair.

Your help is appreciated.

Magento Version 1.8

0

Try to change your foreach to something like this.

    foreach ($collection as $order) {
        $return_variable['order'][] = [
            'order_id' => $order->getId(),
            'increment_id' => $order->increment_id(),
        ];
    }
  • just tried your solution. Result is same as earlier. – Ashish Aug 8 at 9:36
  • Remove cur page and page size limit and try again. – Tsita Aug 8 at 9:40
  • tried this as well. same problem. – Ashish Aug 8 at 9:43
  • Hmm i don't know, try to pass a second parameter in json_encode this constant JSON_UNESCAPED_SLASHES – Tsita Aug 8 at 9:51
  • nope. I think \ is added because of magento at the time of giving response – Ashish Aug 8 at 9:59
0

Finally I gave-up on getting correct JSON format. I used str_replace to remove un-necessary charactors to move forward.

Hope this helps anyone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.