Pregunta

I am using PHP 5.3.15 (cli) (built: Jul 20 2012 23:10:20) on an amazon ec2 instance. When I make a call to json_encode() the resulting JSON randomly skips characters so that the JSON is no longer valid. This happens only some of the time even when the call is made with the exact same parameters/data. The curious thing is that it happens in different spots, random spots, with no pattern. It does not happen with the same data and the same code, and the same version of PHP on my local MAMP installation

..."updateTime":"2012-12-21 03:24:14","createTime":"2012-12-21:"21","updateTime":"2012-12-25 19:33:25",...

You can see the effect above at the 21:"21". I don't even know where to begin solving this problem.

I've rebooted and even created new virtual instances of the server.

I am in grievous need of help. Anything you could suggest would be awesome!!

¿Fue útil?

Solución

you must set json encode Parameter for QUOT or another

 $json=json_encode($array,JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE);

this is a sample of json encode that is on php.net

 <?php
   $a = array('<foo>',"'bar'",'"baz"','&blong&', "\xc3\xa9");

   echo "Normal: ",  json_encode($a), "\n";
   echo "Tags: ",    json_encode($a, JSON_HEX_TAG), "\n";
   echo "Apos: ",    json_encode($a, JSON_HEX_APOS), "\n";
   echo "Quot: ",    json_encode($a, JSON_HEX_QUOT), "\n";
   echo "Amp: ",     json_encode($a, JSON_HEX_AMP), "\n";
   echo "Unicode: ", json_encode($a, JSON_UNESCAPED_UNICODE), "\n";
   echo "All: ",     json_encode($a, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT |JSON_HEX_AMP | JSON_UNESCAPED_UNICODE), "\n\n";

   $b = array();

   echo "Empty array output as array: ", json_encode($b), "\n";
   echo "Empty array output as object: ", json_encode($b, JSON_FORCE_OBJECT), "\n\n";

   $c = array(array(1,2,3));

   echo "Non-associative array output as array: ", json_encode($c), "\n";
   echo "Non-associative array output as object: ", json_encode($c, JSON_FORCE_OBJECT), "\n\n";

    $d = array('foo' => 'bar', 'baz' => 'long');

   echo "Associative array always output as object: ", json_encode($d), "\n";
   echo "Associative array always output as object: ", json_encode($d, JSON_FORCE_OBJECT), "\n\n";
 ?>

output:

 Normal: ["<foo>","'bar'","\"baz\"","&blong&","\u00e9"]
 Tags: ["\u003Cfoo\u003E","'bar'","\"baz\"","&blong&","\u00e9"]
 Apos: ["<foo>","\u0027bar\u0027","\"baz\"","&blong&","\u00e9"]
 Quot: ["<foo>","'bar'","\u0022baz\u0022","&blong&","\u00e9"]
 Amp: ["<foo>","'bar'","\"baz\"","\u0026blong\u0026","\u00e9"]
 Unicode: ["<foo>","'bar'","\"baz\"","&blong&","é"]
 All: ["\u003Cfoo\u003E","\u0027bar\u0027","\u0022baz\u0022","\u0026blong\u0026","é"]

 Empty array output as array: []
 Empty array output as object: {}

 Non-associative array output as array: [[1,2,3]]
 Non-associative array output as object: {"0":{"0":1,"1":2,"2":3}}

 Associative array always output as object: {"foo":"bar","baz":"long"}
 Associative array always output as object: {"foo":"bar","baz":"long"}

Otros consejos

I used a proxy to analyze what was going on. I was making two identical requests right after each other. It may be important to note that the requests were in objective-c and that it may not be php that was breaking, but merely the second request canceling the first.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top