PHP Classes

Is it a bug or a json syntaxe mistake

Recommend this page to a friend!

      JSON  >  All threads  >  Is it a bug or a json syntaxe mistake  >  (Un) Subscribe thread alerts  
Subject:Is it a bug or a json syntaxe mistake
Summary:I juste change objet member position and it does not read other
Messages:1
Author:RANDRIANARISON
Date:2011-08-03 15:02:46
 

  1. Is it a bug or a json syntaxe mistake   Reply   Report abuse  
Picture of RANDRIANARISON RANDRIANARISON - 2011-08-03 15:02:46
Hello,

I have this class structure

class contrat
{
var $m_contrat;
var $m_datecontrat;
}

class control
{
var $isContratOK;
var $isProductOK;
var $isProductSupportOK;

var $m_listContrat = array() // array of object contrat
}

---------------

When i run this PHP code

<?php

require "JSON.php";
$json = new JSON;

echo "<h1>The JSON to php</h1>";

$chaineJSON_1 = "{\"objCtrl\": {\"isContratOK\": true, \"isProduitOK\": true, \"isProduitSupportOK\": false, \"m_listContrat\": [{\"m_contrat\": \"contrat 1\", \"m_datecontrat\": \"01/01/2011\"}, {\"m_contrat\": \"contrat 1\", \"m_datecontrat\": \"01/01/2011\"}]}}";
$controls = $json->unserialize($chaineJSON_1 );
print "chaineJSON_1".$chaineJSON_1."<hr><br/><pre>".print_r($controls,true)."</pre>";

echo $controls->objCtrl->isContratOK."<br />";
echo $controls->objCtrl->m_listContrat[0]->m_datecontrat."<br />";


?>
<script>
try
{
//alert("<?php echo addslashes($var);?>");
obj = eval('(<?php echo addslashes($chaineJSON_1);?>)');
alert("$chaineJSON_1 : " + obj.objCtrl.m_listContrat[0].m_datecontrat);
}
catch (e)
{
alert("Error:" + e);
}
</script>

<?php
$chaineJSON_2 = "{\"objCtrl\": {\"m_listContrat\": [{\"m_contrat\": \"contrat 1\", \"m_datecontrat\": \"01/01/2011\"}, {\"m_contrat\": \"contrat 1\", \"m_datecontrat\": \"01/01/2011\"}], \"isContratOK\": true, \"isProduitOK\": true, \"isProduitSupportOK\": false}}";
$controls = $json->unserialize($chaineJSON_2 );
print "chaineJSON_2".$chaineJSON_2."<hr><pre>".print_r($controls,true)."</pre>";

echo $controls->objCtrl->isContratOK."<br />";
echo $controls->objCtrl->m_listContrat[0]->m_datecontrat."<br />";


?>
<script>
try
{
//alert("<?php echo addslashes($var);?>");
obj = eval('(<?php echo addslashes($chaineJSON_2);?>)');
alert("$chaineJSON_2 : " + obj.objCtrl.m_listContrat[0].m_datecontrat);
}
catch (e)
{
alert("Error:" + e);
}
</script>

the second one do the right thing,
the first one crash (does not read array m_listContrat)

--------------

Here is the result

The JSON to php
chaineJSON_1{"objCtrl": {"isContratOK": true, "isProduitOK": true, "isProduitSupportOK": false, "m_listContrat": [{"m_contrat": "contrat 1", "m_datecontrat": "01/01/2011"}, {"m_contrat": "contrat 1", "m_datecontrat": "01/01/2011"}]}}
--------------------------------------------------------------------------------


stdClass Object
(
[objCtrl] => stdClass Object
(
[isContratOK] => 1
[isProduitOK] => 1
[isProduitSupportOK] =>
)

)

1

chaineJSON_2{"objCtrl": {"m_listContrat": [{"m_contrat": "contrat 1", "m_datecontrat": "01/01/2011"}, {"m_contrat": "contrat 1", "m_datecontrat": "01/01/2011"}], "isContratOK": true, "isProduitOK": true, "isProduitSupportOK": false}}
--------------------------------------------------------------------------------

stdClass Object
(
[objCtrl] => stdClass Object
(
[m_listContrat] => Array
(
[0] => stdClass Object
(
[m_contrat] => contrat 1
[m_datecontrat] => 01/01/2011
)

[1] => stdClass Object
(
[m_contrat] => contrat 1
[m_datecontrat] => 01/01/2011
)

)

[isContratOK] => 1
[isProduitOK] => 1
[isProduitSupportOK] =>
)

)

1
01/01/2011