0

I have problem with JSON, passing html code, I don't understand because I escaped with addslashes php function.

This is the JSON that fail:

With php JSON is valid:

<?php if(count($articles)): ?>
{"items":[
<?php foreach($articles as $key => $article): ?>
      <?php if($key==0  ):?>
      {
        "foto_g": "<?php echo $article->getRutafoto() ?>",
        "foto_th": "<?php echo $article->getRutathumb() ?>"

      }
    <?php else: ?>  
    ,
      {
        "foto_g": "<?php echo $article->getRutafoto() ?>",
        "foto_th": "<?php echo $article->getRutathumb() ?>"

      }
    <?php endif ?>  
<?php endforeach ?>
],
"nom_coleccio": "<?php echo $coleccio->getNom()?>"
,
"descripcio_coleccio": "<?php echo addslashes($coleccio->getDescripcio(ESC_RAW))?>"
}
<?php endif ?>  

And the result that have problem is:

{
"descripcio_coleccio": "<p>El delta de l\'Ebre ha estat l\'escenari d\'inspiraci&oacute; d\'aquesta col&middot;lecci&oacute;.</p>
<p>La l&iacute;nia de l\'horitz&oacute; i el color del paisatge materialitzats en alumini s\'uneixen per a crear volum en forma de joia.</p>"
}

When is the problem?

Thanks Regards

7
  • 1
    Is there a particular reason you are reimplementing json_encode, badly? Commented Feb 18, 2012 at 12:51
  • It looks to me like you're embedding the call to encode your data, not the result of the call. Commented Feb 18, 2012 at 12:54
  • @mario YEs but my strcut is more big, i edited. Commented Feb 18, 2012 at 12:54
  • Maybe you should show what you're really doing. Commented Feb 18, 2012 at 12:55
  • That doesn't explain why you don't create an array structure with your quirky loops first, or at the very least use json_encode for the string values instead of manually enclosing it in " double quotes. Commented Feb 18, 2012 at 12:56

3 Answers 3

3

You should use proper encoding functions if possible. In case of JSON you should use json_encode, even if just for particular values.

But it would be easier if you collect the values in an array with associative keys and use json_encode only at the end:

if (count($articles)) {
    $items = array();
    foreach ($articles as $key => $article) {
        $items[] = array(
            "foto_g"  => $article->getRutafoto(),
            "foto_th" => $article->getRutathumb()
        }
    }
    $data = array(
        "items"               => $items,
        "nom_coleccio"        => $coleccio->getNom(),
        "descripcio_coleccio" => $coleccio->getDescripcio(ESC_RAW)
    );
    echo json_encode($data);
}
Sign up to request clarification or add additional context in comments.

Comments

2

Don't do that! Construct the JSON properly in PHP instead:

<?php
        echo json_encode(array
        (
            "descripcio_coleccio" => $coleccio->getDescripcio(ESC_RAW)
        ));
?>

1 Comment

Yes but my JSON is more big. I eidted post with all JSON.
0

Those single quotes shouldn't be escaped:

{
    "descripcio_coleccio": "<p>Eldeltadel'Ebrehaestatl'escenarid'inspiraci&oacute;d'aquestacol&middot;lecci&oacute;.</p><p>Lal&iacute;niadel'horitz&oacute;ielcolordelpaisatgematerialitzatsenaluminis'uneixenperacrearvolumenformadejoia.</p>"
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.