Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Lorna Mitchell's Blog:
PHP Returning Numeric Values in JSON
Jul 12, 2011 @ 13:41:39

Lorna Mitchell has a quick reminder about an issue in the new joind.in API version - everything was being returned as strings, even integers.

A few weeks later (my inbox is a black hole and it takes a while to process these things) I fell over a throwaway comment to an undocumented constant JSON_NUMERIC_CHECK, and I added the constant name to my todo list. In the time it took for me to actually get around to googling for this, some wonderful person updated the PHP manual page (this is why I love PHP) to include it as a documented option, and someone else had added a user contributed note about using it.

This option, JSON_NUMERIC_CHECK, tells the json_encode function to property consider numbers in its encoding process. It applies globally, so if there's an instance where you don't want something assigned as a number, you might need to go with another, more flexible JSON encoding option. You can find information about this and other possible options json_encode can take on its manual page.

tagged: jsonencode numeric integer string return encode jsonnumericcheck

Link:

Lukas Smith's Blog:
Lets talk about JSON in Symfony2
Oct 28, 2010 @ 17:13:43

Lukas Smith has a new post to his blog looking at some of his experiences with working with JSON in the upcoming version of the Symfony framework (Symfony2), specifically in using the DoctrineUserBundle and load views via JSON.

Unfortunately right now there are quite a few issues with taking a 3rd party Bundle which wasn't specifically build to be accessible via JSON. So in the end I build a MultiplexController that can JSON-ify any controller. The name derives from the fact that it also supports calling multiple routes at once and returning all the data from all these routes in one request. But being good OSS citizens here at Liip, we decided to use this opportunity to try and come up with a more sustainable solution. The goal would be to make it essentially possible to have a 3rd party controller return JSON without having to do anything on the controller level.

He talks about the "magic" that lets you define the output format you'd like and how it works on the backend with the templating system to use templates based on the requested file. He points out a few issues with the method - a form request problem and that there's no good way to call json_encode from inside a twig template (as well as redirecting after submission).

tagged: json symfony2 issues opinion suggestion jsonencode

Link:

Pablo Viquez' Blog:
How to use json_encode with ISO-8859-1 data – Part2
Aug 03, 2009 @ 12:57:10

As he mentioned in a previous entry on his blog, Pablo Viquez found a few issues with character encoding and the json_encode function. He revisits this in a second look at getting it to cooperate with ISO-8859-1 data.

One solution that I did, in order to preserve the character set was to encode the data before using the json_encode function to use just A-Z, a-z and 0-9 characters, instead of sending text with accents or symbols. One encoding that fits perfectly in this schema, is Base64 Content-Transfer-Encoding. (see base 64 explanation below) This leads me to the solution: Encode the ISO-8859-1 using base64 and decoded in the client using JavaScript.

He includes both a code example for his solution and downloadable code for you to try it out yourself.

tagged: jsonencode iso88591 javascript tutorial

Link:

Pablo Viquez's Blog:
JSON, ISO 8859-1 and UTF-8 - Part
Jul 20, 2009 @ 17:42:36

After spotting some null values in a few of his form fields following an Ajax request, Pablo Viquez decided to track down his issue:

While I was looking at some AJAX calls, I started to have a problem, for some reason, when I tried to query a JSON service I did using JQuery, the result was null for some fields. Going a little deeper, I notice that the records from the DB were OK, and the JavaScript was OK to, so what was the problem? The JSON Encode!

His issue stemmed from the character encoding of the string being passed into json_encode (in this case, UTF-8) was coming from a PHP script saved in a page encoded as ISO-8859-1. You can download the files he's come up with to illustrate the point.

tagged: json encode utf8 iso88591 jsonencode

Link:

ProDevTips.com:
jQuery JSON with PHP json_encode and json_decode
Aug 18, 2008 @ 15:22:04

On the ProDevTips blog, Henrik shows an example of the "perfect combination" of PHP's json functionality and a powerful javascript library, jQuery.

Today I looked for a replacement for my old jspanserializer.js script that I can't even remember where I found anymore. Turns out that I wont have to either, I can forget it. From now on I'll use jQuery JSON instead.

A code example is included to show the simplicity.

tagged: json jquery jsonencode jsondecode example

Link:

ProDevTips Blog:
Fluent Arrays and Strings in PHP - Adding JSON and more
Jul 17, 2008 @ 14:32:26

Henrik continues his look at fluent arrays and strings in PHP with this second part of his series, adding JSON functionality into the mix.

Recently I've had the need to extend the interface further, among other things with JSON support. I've used code published by Nicolas Crovatti to do this.

His example shows how the script works to plot out some points for a table of stats. He follows this with an in-depth look at the different methods making up that fluent call (apply2Field, fill_ret, sum and plot). These make the array that can then be passed off to PHP, using json_encode to make an easy-consumable JSON message out of the results.

tagged: json fluent array string php5 tutorial jsonencode

Link:


Trending Topics: