Formatting output with json_encode

Formatting output with json_encode

am 14.07.2008 08:21:23 von Ron Piggott

I am using json_encode to output the results of a mySQL query which is
part of a jquery & ajax.

$result = json_encode($array);
echo $result;

The output on my screen is:

{0:"John 14:27","verse_reference":"John 14:27"}

How do I format json_encode output? I would like

John 14:27

to be what is displayed.

Ron


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Formatting output with json_encode

am 14.07.2008 11:30:38 von Evert Lammerts

Check out http://www.json.org/ for JSON structures.

In javascript an Array object is a special implementation of the
Object object (like all objects other than Object) - an Array object
can only have numerical indices, an Object object can have textual
indices too. If you json_encode a php array with textual indices it
translates to {...} - a javascript Object object. If you json_encode a
php array with numerical indices it translates to [...] - a javascript
Array object.

It seems to me that what you're using is mysql_fetch_array
(http://nl3.php.net/manual/en/function.mysql-fetch-array.php ) with
either no result_type parameter set, or result_type set to MYSQL_BOTH.
If you want a javascript Array object, use either
mysql_fetch_array($result, MYSQL_NUM) or mysql_fetch_row($result). If
you want a javascript Object object, use either
mysql_fetch_array($result, MYSQL_ASSOC) or mysql_fecth_assoc($result).

Evert

On Mon, Jul 14, 2008 at 8:21 AM, Ron Piggott wrote:
> I am using json_encode to output the results of a mySQL query which is
> part of a jquery & ajax.
>
> $result = json_encode($array);
> echo $result;
>
> The output on my screen is:
>
> {0:"John 14:27","verse_reference":"John 14:27"}
>
> How do I format json_encode output? I would like
>
> John 14:27
>
> to be what is displayed.
>
> Ron
>
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php