PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > php.general > json_encode/json_decode, take 2
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
json_encode/json_decode, take 2

Réponse
 
LinkBack Outils de la discussion
Vieux 19/10/2007, 17h51   #1
Christoph Boget
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut json_encode/json_decode, take 2

The string used below in "$myArrEncoded" is generated in javascript, after
creating the structure and spitting out:

var JSONVar = javascriptVar.toSource();

I can eval JSONVar and work with it as I would be working with the original
javascriptVar so I know the transition back and forth from a structure to a
string isn't causing problems. The problem is when I try to work with the
string in PHP.

Here is my code:

<?php

$myArrEncoded = "({Salary:{'50-70K':{filterType:&quot;range&quot;,
fieldName:&quot;SALARY&quot;, fieldValueLabel:&quot;50-70K&quot;,
lowerValue:&quot;50&quot;, upperValue:&quot;70&quot;,
inclusive:&quot;BOTH&quot;}},
Position:{Developer:{filterType:&quot;single&quot; ,
fieldName:&quot;POSITION&quot;, fieldValueLabel:&quot;Developer&quot;,
fieldValue:&quot;Developer&quot;, constraint:&quot;equals&quot;},
SysAdmin:{filterType:&quot;single&quot;, fieldName:&quot;POSITION&quot;,
fieldValueLabel:&quot;System Admin&quot;, fieldValue:&quot;SysAdmin&quot;,
constraint:&quot;equals&quot;}}, 'Required Action':{'2 -
GenScreen':{filterType:&quot;single&quot;,
fieldName:&quot;REQUIRED_ACTION&quot;, fieldValueLabel:&quot;General Phone
Screen&quot;, fieldValue:&quot;2 - GenScreen&quot;,
constraint:&quot;equals&quot;}}})";

echo var_dump( $myArrEncoded ) . '<br><br>';
echo '$myArr encoded: ' . $myArrEncoded . '<br><br>';
try {
echo '$myArr decoded: <pre>' . print_r( json_decode( $myArrEncoded, TRUE
), TRUE ) . '</pre><br><br>';
} catch( Exception $e ) {
echo 'Error: ' . var_dump( $e );
}

?>

When I run it, I get the following output:

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

string(587) "({Salary:{'50-70K':{filterType:"range",
fieldName:"SALARY", fieldValueLabel:"50-70K", lowerValue:"50",
upperValue:"70", inclusive:"BOTH"}},
Position:{Developer:{filterType:"single", fieldName:"POSITION",
fieldValueLabel:"Developer", fieldValue:"Developer",
constraint:"equals"}, SysAdmin:{filterType:"single",
fieldName:"POSITION", fieldValueLabel:"System Admin",
fieldValue:"SysAdmin", constraint:"equals"}}, 'Required Action':{'2 -
GenScreen':{filterType:"single", fieldName:"REQUIRED_ACTION",
fieldValueLabel:"General Phone Screen", fieldValue:"2 - GenScreen",
constraint:"equals"}}})"

$myArr encoded: ({Salary:{'50-70K':{filterType:"range",
fieldName:"SALARY", fieldValueLabel:"50-70K", lowerValue:"50",
upperValue:"70", inclusive:"BOTH"}},
Position:{Developer:{filterType:"single", fieldName:"POSITION",
fieldValueLabel:"Developer", fieldValue:"Developer",
constraint:"equals"}, SysAdmin:{filterType:"single",
fieldName:"POSITION", fieldValueLabel:"System Admin",
fieldValue:"SysAdmin", constraint:"equals"}}, 'Required Action':{'2 -
GenScreen':{filterType:"single", fieldName:"REQUIRED_ACTION",
fieldValueLabel:"General Phone Screen", fieldValue:"2 - GenScreen",
constraint:"equals"}}})

$myArr decoded: ({Salary:{'50-70K':{filterType:"range",
fieldName:"SALARY", fieldValueLabel:"50-70K", lowerValue:"50",
upperValue:"70", inclusive:"BOTH"}},
Position:{Developer:{filterType:"single", fieldName:"POSITION",
fieldValueLabel:"Developer", fieldValue:"Developer",
constraint:"equals"}, SysAdmin:{filterType:"single",
fieldName:"POSITION", fieldValueLabel:"System Admin",
fieldValue:"SysAdmin", constraint:"equals"}}, 'Required Action':{'2 -
GenScreen':{filterType:"single", fieldName:"REQUIRED_ACTION",
fieldValueLabel:"General Phone Screen", fieldValue:"2 - GenScreen",
constraint:"equals"}}})

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

Why isn't json_decode() converting the string to an array? Is there
something extra in there that json_decode() can't deal with? I can work
with it fine in javascript...

thnx,
Christoph

  Réponse avec citation
Vieux 19/10/2007, 21h59   #2
Jochem Maas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] json_encode/json_decode, take 2

Christoph Boget wrote:
> The string used below in "$myArrEncoded" is generated in javascript, after
> creating the structure and spitting out:
>
> var JSONVar = javascriptVar.toSource();
>
> I can eval JSONVar and work with it as I would be working with the original
> javascriptVar so I know the transition back and forth from a structure to a
> string isn't causing problems. The problem is when I try to work with the
> string in PHP.
>
> Here is my code:


&quot; is not what you think it is (but that could be my mail client),
and the exterior parentheses are also incorrect. your try/catch block is doing
nothing because neither function throws exceptions.
  Réponse avec citation
Vieux 19/10/2007, 22h12   #3
Nathan Nobbe
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] json_encode/json_decode, take 2

On 10/19/07, Jochem Maas <jochem@iamjochem.com> wrote:
>
> Christoph Boget wrote:
> > The string used below in "$myArrEncoded" is generated in javascript,

> after
> > creating the structure and spitting out:
> >
> > var JSONVar = javascriptVar.toSource();
> >
> > I can eval JSONVar and work with it as I would be working with the

> original
> > javascriptVar so I know the transition back and forth from a structure

> to a
> > string isn't causing problems. The problem is when I try to work with

> the
> > string in PHP.
> >
> > Here is my code:

>
> &quot; is not what you think it is (but that could be my mail client),
> and the exterior parentheses are also incorrect. your try/catch block is
> doing
> nothing because neither function throws exceptions.



i thought it might be something like that, but i didnt spend long enough at
it to verify.
here is a great tool to :

http://www.jslint.com/

-nathan

  Réponse avec citation
Vieux 19/10/2007, 22h20   #4
Jochem Maas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] json_encode/json_decode, take 2

try seeing what json_encode() creates:

$json = "json";
$data = array(
"no_1" => $json,
"no_2" => $json,
"no_3" => $json,
"no_4" => array("foo" => $json, "bar" = $json),
);
print_r({"{$json}_encode"}($data));


Christoph Boget wrote:
> The string used below in "$myArrEncoded" is generated in javascript, after
> creating the structure and spitting out:
>
> var JSONVar = javascriptVar.toSource();
>
> I can eval JSONVar and work with it as I would be working with the original
> javascriptVar so I know the transition back and forth from a structure to a
> string isn't causing problems. The problem is when I try to work with the
> string in PHP.
>
> Here is my code:
>
> <?php
>
> $myArrEncoded = "({Salary:{'50-70K':{filterType:&quot;range&quot;,
> fieldName:&quot;SALARY&quot;, fieldValueLabel:&quot;50-70K&quot;,
> lowerValue:&quot;50&quot;, upperValue:&quot;70&quot;,
> inclusive:&quot;BOTH&quot;}},
> Position:{Developer:{filterType:&quot;single&quot; ,
> fieldName:&quot;POSITION&quot;, fieldValueLabel:&quot;Developer&quot;,
> fieldValue:&quot;Developer&quot;, constraint:&quot;equals&quot;},
> SysAdmin:{filterType:&quot;single&quot;, fieldName:&quot;POSITION&quot;,
> fieldValueLabel:&quot;System Admin&quot;, fieldValue:&quot;SysAdmin&quot;,
> constraint:&quot;equals&quot;}}, 'Required Action':{'2 -
> GenScreen':{filterType:&quot;single&quot;,
> fieldName:&quot;REQUIRED_ACTION&quot;, fieldValueLabel:&quot;General Phone
> Screen&quot;, fieldValue:&quot;2 - GenScreen&quot;,
> constraint:&quot;equals&quot;}}})";
>
> echo var_dump( $myArrEncoded ) . '<br><br>';
> echo '$myArr encoded: ' . $myArrEncoded . '<br><br>';
> try {
> echo '$myArr decoded: <pre>' . print_r( json_decode( $myArrEncoded, TRUE
> ), TRUE ) . '</pre><br><br>';
> } catch( Exception $e ) {
> echo 'Error: ' . var_dump( $e );
> }
>
> ?>
>
> When I run it, I get the following output:
>
> --------------------------------------------------------------------------------
>
> string(587) "({Salary:{'50-70K':{filterType:"range",
> fieldName:"SALARY", fieldValueLabel:"50-70K", lowerValue:"50",
> upperValue:"70", inclusive:"BOTH"}},
> Position:{Developer:{filterType:"single", fieldName:"POSITION",
> fieldValueLabel:"Developer", fieldValue:"Developer",
> constraint:"equals"}, SysAdmin:{filterType:"single",
> fieldName:"POSITION", fieldValueLabel:"System Admin",
> fieldValue:"SysAdmin", constraint:"equals"}}, 'Required Action':{'2 -
> GenScreen':{filterType:"single", fieldName:"REQUIRED_ACTION",
> fieldValueLabel:"General Phone Screen", fieldValue:"2 - GenScreen",
> constraint:"equals"}}})"
>
> $myArr encoded: ({Salary:{'50-70K':{filterType:"range",
> fieldName:"SALARY", fieldValueLabel:"50-70K", lowerValue:"50",
> upperValue:"70", inclusive:"BOTH"}},
> Position:{Developer:{filterType:"single", fieldName:"POSITION",
> fieldValueLabel:"Developer", fieldValue:"Developer",
> constraint:"equals"}, SysAdmin:{filterType:"single",
> fieldName:"POSITION", fieldValueLabel:"System Admin",
> fieldValue:"SysAdmin", constraint:"equals"}}, 'Required Action':{'2 -
> GenScreen':{filterType:"single", fieldName:"REQUIRED_ACTION",
> fieldValueLabel:"General Phone Screen", fieldValue:"2 - GenScreen",
> constraint:"equals"}}})
>
> $myArr decoded: ({Salary:{'50-70K':{filterType:"range",
> fieldName:"SALARY", fieldValueLabel:"50-70K", lowerValue:"50",
> upperValue:"70", inclusive:"BOTH"}},
> Position:{Developer:{filterType:"single", fieldName:"POSITION",
> fieldValueLabel:"Developer", fieldValue:"Developer",
> constraint:"equals"}, SysAdmin:{filterType:"single",
> fieldName:"POSITION", fieldValueLabel:"System Admin",
> fieldValue:"SysAdmin", constraint:"equals"}}, 'Required Action':{'2 -
> GenScreen':{filterType:"single", fieldName:"REQUIRED_ACTION",
> fieldValueLabel:"General Phone Screen", fieldValue:"2 - GenScreen",
> constraint:"equals"}}})
>
> --------------------------------------------------------------------------------
>
> Why isn't json_decode() converting the string to an array? Is there
> something extra in there that json_decode() can't deal with? I can work
> with it fine in javascript...
>
> thnx,
> Christoph
>

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 22h24.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14284 seconds with 12 queries