Search code examples
phpjsonzend-framework

Zend_Json::encode trouble with Double Quotes


I have a Zend Form that allows you to add a college class to the database. I collect the data and persist it with Doctrine 2. Everything is fine the data is in the table. When I retrieve the data everything is ready to use.

array
   0 => &
array
  'id' => int 151
  'className' => string 'Geocaching (Jee-oh-Cash-ing) is part of a worldwide outdoor  game for GPS users. Go on an adventure to find hidden treasure, called “geocaches”. If you own a GPS receiver or a smartphone, bring it (preferred, but not required) along with some fresh batteri' (length=255)
  'instructor' => string 'Geocaching (Jee-oh-Cash-ing) is part of a worldwide outdoor game for GPS users. Go on an adventure to find hidden treasure, called “geocaches”. If you own a GPS receiver or a smartphone, bring it (preferred, but not required) along with some fresh batteri' (length=255)
  'classDescription' => string 'Geocaching (Jee-oh-Cash-ing) is part of a worldwide outdoor game for GPS users. Go on an adventure to find hidden treasure, called “geocaches”. If you own a GPS receiver or a smartphone, bring it (preferred, but not required) along with some fresh batteri' (length=255)

Then I am using Jquery DataTables to display all the table data. I have a view helper that renders the jquery for the datatable. Inside the view helper I am using

Zend_Json::encode(array_merge($this->_defaultOptions, $options), false,   array('enableJsonExprFinder' => true)); 

All the values that have a double quote get encoded as null.

"aaData":{"id":151,"className":null,"instructor":null,"classDescription":null,}}'

Any other values will display in the DataTable except any value that has a double quote.

I must being doing something really wrong because I also have this trouble when I try to re-populate the Zend Form with the data to do an update.

$results = $this->_doctrine->getEntityManager()->getRepository('My\Entity')->findOneBy($request->getParam('id'));
$form->setDefaults($results[0]);

Again if I dump the results from Doctrine all the quoted data is there ready to be used. But after $form->setDefaults($results[0]) the fields in the form are blank.

Any help is really appreciated.


Solution

  • I had the same problem. The solution is that the quotes were not the " but rather “ (the Microsoft encoded quotes) which was causing json_encode() to return null. Doing a replace with the method from this answer (How to replace Microsoft-encoded quotes in PHP) fixed it.

    UPDATE:

    Zend also has an encoder that parses the string for you. But you need to set Zend_Json::$userBuiltinEncoderDecoder = true in your bootstrap and it will then use it instead of php's json_encode