Page 1 sur 1

json_encode object

Posté : 15 nov. 2015, 16:47
par kodjo
Bonjour à tous,

J'ai l'objet suivant à traiter:

Code : Tout sélectionner

JForm Object ( [data:protected] => Joomla\Registry\Registry Object ( [data:protected] => stdClass Object ( ) [separator] => . ) [errors:protected] => Array ( ) [name:protected] => item_component [options:protected] => Array ( [control] => ) [xml:protected] => SimpleXMLElement Object ( [fields] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => params [label] => COM_MENUS_LINKTYPE_OPTIONS_LABEL ) [fieldset] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => menu-options [label] => COM_MENUS_LINKTYPE_OPTIONS_LABEL ) [field] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => menu-anchor_title [type] => text [label] => COM_MENUS_ITEM_FIELD_ANCHOR_TITLE_LABEL [description] => COM_MENUS_ITEM_FIELD_ANCHOR_TITLE_DESC ) ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => menu-anchor_css [type] => text [label] => COM_MENUS_ITEM_FIELD_ANCHOR_CSS_LABEL [description] => COM_MENUS_ITEM_FIELD_ANCHOR_CSS_DESC ) ) [2] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => menu_image [type] => media [label] => COM_MENUS_ITEM_FIELD_MENU_IMAGE_LABEL [description] => COM_MENUS_ITEM_FIELD_MENU_IMAGE_DESC ) ) [3] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => menu_text [type] => radio [class] => btn-group btn-group-yesno [label] => COM_MENUS_ITEM_FIELD_MENU_TEXT_LABEL [description] => COM_MENUS_ITEM_FIELD_MENU_TEXT_DESC [default] => 1 [filter] => integer ) [option] => Array ( [0] => JYES [1] => JNO ) ) ) ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => page-options [label] => COM_MENUS_PAGE_OPTIONS_LABEL ) [field] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => page_title [type] => text [label] => COM_MENUS_ITEM_FIELD_PAGE_TITLE_LABEL [description] => COM_MENUS_ITEM_FIELD_PAGE_TITLE_DESC ) ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => show_page_heading [type] => list [class] => chzn-color [label] => COM_MENUS_ITEM_FIELD_SHOW_PAGE_HEADING_LABEL [description] => COM_MENUS_ITEM_FIELD_SHOW_PAGE_HEADING_DESC [default] => ) [option] => Array ( [0] => JGLOBAL_USE_GLOBAL [1] => JYES [2] => JNO ) ) [2] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => page_heading [type] => text [label] => COM_MENUS_ITEM_FIELD_PAGE_HEADING_LABEL [description] => COM_MENUS_ITEM_FIELD_PAGE_HEADING_DESC ) ) [3] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => pageclass_sfx [type] => text [label] => COM_MENUS_ITEM_FIELD_PAGE_CLASS_LABEL [description] => COM_MENUS_ITEM_FIELD_PAGE_CLASS_DESC ) ) ) ) [2] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => metadata [label] => JGLOBAL_FIELDSET_METADATA_OPTIONS ) [field] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => menu-meta_description [type] => textarea [label] => JFIELD_META_DESCRIPTION_LABEL [description] => JFIELD_META_DESCRIPTION_DESC [rows] => 3 [cols] => 40 ) ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => menu-meta_keywords [type] => textarea [label] => JFIELD_META_KEYWORDS_LABEL [description] => JFIELD_META_KEYWORDS_DESC [rows] => 3 [cols] => 40 ) ) [2] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => robots [type] => list [label] => JFIELD_METADATA_ROBOTS_LABEL [description] => JFIELD_METADATA_ROBOTS_DESC ) [option] => Array ( [0] => JGLOBAL_USE_GLOBAL [1] => JGLOBAL_INDEX_FOLLOW [2] => JGLOBAL_NOINDEX_FOLLOW [3] => JGLOBAL_INDEX_NOFOLLOW [4] => JGLOBAL_NOINDEX_NOFOLLOW ) ) [3] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => secure [type] => list [label] => COM_MENUS_ITEM_FIELD_SECURE_LABEL [description] => COM_MENUS_ITEM_FIELD_SECURE_DESC [default] => 0 [filter] => integer ) [option] => Array ( [0] => JOFF [1] => JON [2] => COM_MENUS_FIELD_VALUE_IGNORE ) ) ) ) ) ) ) [repeat] => ) object(stdClass)#209 (1) { ["repeat"]=> bool(false) }
Je fais un json_encode , puis un json_decode sur l'objet encodé pour voir ce qui sort:

Code : Tout sélectionner

$encoded = json_encode($obj); $decoded = json_decode($encoded); var_dump($decoded);
Le résultat du var_dump est inattendu. J'obtiens:
object(stdClass)#209 (1) {
["repeat"]=> bool(false)
}
Pourquoi l'objet a-t-il été complètement détruit par la fonction Json_encode?
En vous remerciant pour vos réponses.

Re: json_encode object

Posté : 16 nov. 2015, 13:23
par ynx
Salut,

Par défaut json_encode ignore les attributs protected et private d'une classe.
Quel est le besoin de passer par du json ?

Bonne journée

Re: json_encode object

Posté : 16 nov. 2015, 13:53
par kodjo
Salut, merci pour cette info. J'ai besoin d'envoyer l'objet via une requête ajax.

Re: json_encode object

Posté : 16 nov. 2015, 14:17
par @rthur
Bonjour,

Une piste pour contourner cela ici :
https://secure.php.net/manual/fr/functi ... .php#96248

Re: json_encode object

Posté : 19 nov. 2015, 13:34
par kodjo
Très utile, merci beaucoup.