1

I have a module that populate commerce cart product titles. The function looks like this:

   function spcart_get(){    
  global $user;
$order = commerce_cart_order_load($user->uid);
$wrapper = entity_metadata_wrapper('commerce_order', $order);
//var_dump(commerce_line_item_load(176)->line_item_label);
$line_item_id_sp;
$cart_product_array = array();
for ($i=0 ; $i < sizeof($order->commerce_line_items['und']); $i++){
    $line_item_id_sp = $order->commerce_line_items['und'][$i]['line_item_id'];
    $line_item = commerce_line_item_load($line_item_id_sp);
    $cart_product_array[] = commerce_product_load($line_item->commerce_product['und'][0]['product_id'])->title;
}

$output = (object) array('products' => $cart_product_array);
return drupal_json_output($output);
}

but the output of the function is in ascii: {"products":["\u05d0\u05d5\u05e8\u05d6","\u05d0\u05d5\u05e8\u05d6"]}

I want that the output json will be un regular UTF-8 letters... What is the problem?

2 Answers 2

0

drupal_json_output actually echos out the code its self so you don't need to return it's output, but you do need to exit after calling it:

drupal_json_output($output);
drupal_exit();

I'm not sure if this will fix your problem but certainly won't hurt...

0

You can probably blame drupal_json_encode_helper(), and the fact that you're running a PHP version < 5.3. That function is used in place of json_encode() for earlier versions of PHP, and, rightly or wrongly, escapes unicode characters in the way you're describing.

If I recall correctly you're right, this is an unnecessary (and evidently annoying, sometimes) step. JSON's preferred encoding is UTF-8, and there are only one or two forbidden characters.

You can probably get around it by using plain old echo json_encode($output); instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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