Forums / Cotonti / Development / Help request :: JSON

JSON notation

ez
#1 2010-04-07 05:13
Hi all,

I am trying to convert a SQL result to JSON (string)

JSON Function: json_encode($arr)
see: http://www.php.net/manual/en/function.json-encode.php

The return of an sed_sql_query is a resource, so i cannot use that function...
How can I do this ???

Any help is appreciated :)
==- I say: Keep it EZ -==
tensh
#2 2010-04-07 15:19
Make a while($row = sed_sql_fetcharray($sql)) {} loop and create your own array?
ez
#3 2010-04-08 02:11
I have figured it out... For the developers, here's the answer:

	// query return to JSON
	$tmpjson="";
	while($irow = sed_sql_fetchassoc($isql)) {
		$tmpjson .= '"id'.$irow['ci_id'].'":'.json_encode($irow).',';
	}
	$tmpjson="{".substr($tmpjson,0,-1)."}";

The following part: '"id'.$irow['ci_id'] You must decide yourself how it is named
for me idxxx where xxx is a unique rowid was usefull.


I find JSON an effective way to transfer data from AJAX requests.
with JSON you can process complex structures quickly into your screen :)

Hope more developers will use it !

Have FUN
==- I say: Keep it EZ -==
Sergey
#4 2010-04-08 12:47
It is my test JSON method
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title></title>
</head>

<body>

<?php

//$json = '{"a":"hellow","b":2,"c":3,"d":{"d":10,"1":20},"e":"при\"вет"}';
$a = array('<aaa>',"'bar'",'"baz"','&blong&',"при\"вет");

echo "исходный: array('<aaa>',\"'bar'\",'\"baz\"','&blong&',\"при\\\"вет\"):", print_r($a), "<br />";
echo "Normal: ", json_encode($a), "<br />";
echo "Tags: ",   json_encode($a,JSON_HEX_TAG), "<br />";
echo "Apos: ",   json_encode($a,JSON_HEX_APOS), "<br />";
echo "Quot: ",   json_encode($a,JSON_HEX_QUOT), "<br />";
echo "Amp: ",    json_encode($a,JSON_HEX_AMP), "<br />";
echo "All: ",    json_encode($a,JSON_HEX_TAG|JSON_HEX_APOS|JSON_HEX_QUOT|JSON_HEX_AMP), "<br /><br />";


//echo "Zend json: ",    Zend_Json::encode($a), "<br /><br />";

$b = array();
echo "исходный: array():", print_r($b), "<br />";
echo "Empty array output as array: ", json_encode($b), "<br />";
echo "Empty array output as object: ", json_encode($b, JSON_FORCE_OBJECT), "<br /><br />";

$c = array(array(1,2,3));
echo "исходный: array(array(1,2,3):", print_r($c), "<br />";
echo "Non-associative array output as array: ", json_encode($c), "\n";
echo "Non-associative array output as object: ", json_encode($c, JSON_FORCE_OBJECT), "<br /><br />";
/*

исходный: array('',"'bar'",'"baz"','&blong&',"при\"вет"):Array ( [0] => [1] => 'bar' [2] => "baz" [3] => &blong& [4] => при"вет ) 1
Normal: ["","'bar'","\"baz\"","&blong&","\u043f\u0440\u0438\"\u0432\u0435\u0442"]
Tags: ["\u003Caaa\u003E","'bar'","\"baz\"","&blong&","\u043f\u0440\u0438\"\u0432\u0435\u0442"]
Apos: ["","\u0027bar\u0027","\"baz\"","&blong&","\u043f\u0440\u0438\"\u0432\u0435\u0442"]
Quot: ["","'bar'","\u0022baz\u0022","&blong&","\u043f\u0440\u0438\u0022\u0432\u0435\u0442"]
Amp: ["","'bar'","\"baz\"","\u0026blong\u0026","\u043f\u0440\u0438\"\u0432\u0435\u0442"]
All: ["\u003Caaa\u003E","\u0027bar\u0027","\u0022baz\u0022","\u0026blong\u0026","\u043f\u0440\u0438\u0022\u0432\u0435\u0442"]

исходный: array():Array ( ) 1
Empty array output as array: []
Empty array output as object: {}

исходный: array(array(1,2,3):Array ( [0] => Array ( [0] => 1 [1] => 2 [2] => 3 ) ) 1
Non-associative array output as array: [[1,2,3]] Non-associative array output as object: {"0":{"0":1,"1":2,"2":3}}

броузер FF
исходный: array('<aaa>',"'bar'",'"baz"','&blong&',"при\"вет"):Array
(
    [0] => <aaa>
    [1] => 'bar'
    [2] => "baz"
    [3] => &blong&
    [4] => при"вет
)
1<br />Normal: ["<aaa>","'bar'","\"baz\"","&blong&","\u043f\u0440\u0438\"\u0432\u0435\u0442"]<br />
Tags: ["\u003Caaa\u003E","'bar'","\"baz\"","&blong&","\u043f\u0440\u0438\"\u0432\u0435\u0442"]<br />
Apos: ["<aaa>","\u0027bar\u0027","\"baz\"","&blong&","\u043f\u0440\u0438\"\u0432\u0435\u0442"]<br />
Quot: ["<aaa>","'bar'","\u0022baz\u0022","&blong&","\u043f\u0440\u0438\u0022\u0432\u0435\u0442"]<br />
Amp: ["<aaa>","'bar'","\"baz\"","\u0026blong\u0026","\u043f\u0440\u0438\"\u0432\u0435\u0442"]<br />
All: ["\u003Caaa\u003E","\u0027bar\u0027","\u0022baz\u0022","\u0026blong\u0026","\u043f\u0440\u0438\u0022\u0432\u0435\u0442"]<br /><br />

исходный: array():Array
(
)
1<br />Empty array output as array: []<br />Empty array output as object: {}<br /><br />исходный: array(array(1,2,3):Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

)
1<br />Non-associative array output as array: [[1,2,3]]
Non-associative array output as object: {"0":{"0":1,"1":2,"2":3}}<br /><br />



*/

?>

</body>

</html>

Добавлено 5 дней спустя:

The national coding function generates json_encode 6 characters on 1 national symbol (see post above)
that is:["<aaa>","'bar'","\"baz\"","&blong&","\u043f\u0440\u0438\"\u0432\u0435\u0442"]
for: $a = array('<aaa>',"'bar'",'"baz"','&blong&',"при\"вет");
This function utf8_jsondecode eliminates this problem
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title></title>
</head>

<body>

<?php

$a = array('<aaa>',"'bar'",'"baz"','&blong&',"при\"вет");

echo "исходный: array('\<aaa\>',\"'bar'\",'\"baz\"','&blong&',\"при\\\"вет\"):", print_r($a), "<br />";
echo "Normal: ", $str=json_encode($a), "<br />";

function utf8_jsondecode($str) {
    $str = preg_replace("/\\\u([0-9a-f]{1,4})/i","&#x\\1;",urldecode($str));
    return html_entity_decode($str,null,'UTF-8');;
  }

echo "unicode_decode: ", utf8_jsondecode($str), "<br />";

/*

броузер FF

исходный: array('\<aaa\>',"'bar'",'"baz"','&blong&',"при\"вет"):
Array
(
    [0] => <aaa>
    [1] => 'bar'
    [2] => "baz"
    [3] => &blong&
    [4] => при"вет
)
1

Normal: ["<aaa>","'bar'","\"baz\"","&blong&","\u043f\u0440\u0438\"\u0432\u0435\u0442"]
unicode_decode: ["<aaa>","'bar'","\"baz\"","&blong&","при\"вет"]
*/

?>
</body>

</html>
www.cotonti.mobi
This post was edited by Sergey (2010-04-13 23:12, 14 years ago)
ez
#5 2010-04-14 04:59
Hi Sergey, i am using something like:

function utf16_urlencode ( $str ) {
        # convert characters > 255 into HTML entities
        $convmap = array( 0xFF, 0x2FFFF, 0, 0xFFFF );
        $str = mb_encode_numericentity( $str, $convmap, "UTF-8");

        # escape HTML entities, so they are not urlencoded
        $str = preg_replace( '/&#([0-9a-fA-F]{2,5});/i', 'mark\\1mark', $str );
        $str = urlencode($str);

        # now convert escaped entities into unicode url syntax
        $str = preg_replace( '/mark([0-9a-fA-F]{2,5})mark/i', '%u\\1', $str );
        return $str;
    }
==- I say: Keep it EZ -==
Kilandor
#6 2010-04-16 20:20
$sql = sed_sql_query("SOME QUERY");
//echo json_encode($sql); this returns a resource id # because thats what it is a referrence for the results basicly

while($row = sed_sql_fetcharray($sql))
{
  //echo json_encode($row);
  //This would output the data for a row, but yes would not work for multiple rows however you could do this
  $json_data[] = $row;
}
echo json_encode($json_data);
//This would create an array and output it for each sql row such as data.0.sqldata

This may be a little easier, Ez since you do not have to build your own jsonarray

AJAX and json data can be quite nice sometimes. I use it alot in my CLI plugin on my site actually functions completely revolves around AJAX/JS
ez
#7 2010-04-16 22:48
Super... I will try that.

I am starting to really use Ajax and JSON... it is great stuff, and fast, and no constant screen refreshes.. Well offcourse I don't have to tell you :)
==- I say: Keep it EZ -==