pregunta sobre php decodificar JSON

I still have some question about php decode JSON. the JSON return like this.

  "DocTitle":"Image: A municipal police officer takes positio",
          "DocExpire":"2/7/2011 1:39:02 PM"

this is my php code:

foreach ($data->Documents as $result) { 
    echo htmlspecialchars($result->DocTitle).'<br />';
        echo htmlspecialchars($result->Docmultimedia->DocExpire).'<br />';

Vuelve Warning: Invalid argument supplied for foreach(). and echo htmlspecialchars($result->Docmultimedia->DocExpire), is it write right? Thanks all.

preguntado el 08 de enero de 11 a las 22:01

It seems you are not processing the string correctly. Otherwise $data->Documents would return an array. -

Parece JSONP a mi. -

Has comprobado $data no es false y $data->Documents ¿no está vacío? -

debido a la all(...) at the head of the JSON data? sorry for I am a newbie. -

que es eso all(...) thing? invlid json? -

2 Respuestas

Condición previa: The question is based on an MSNBC api (

API Call
You should call the API without the trailing &jsonp=all, this will make the json evaluateable

API Result MSN returns some values with NaN, NaN is no valid JSON as JSONLint demostrado.

Result Digesting
I provide a ejemplo de trabajo on GIST for digesting.

Docmultimedia is an array, as indicated by "Docmultimedia": [ {...} ] tienes que llamar $result->Docmultimedia[0]->DocExpire to retrieve it. alternatively iterate over it.

I noticed that in the APIs result no Docmultimedia occurs ever, instead DocExpire is an property of the document. Retrieve it via $result->DocExpire.

Respondido el 09 de enero de 11 a las 03:01

@cj333 could you post a (reduced) code sample on either gist or codepad (where felix and mine examples are). i could look into the code then. - Samuel Herzog

@cj333: I get this error: "Latitude":NaN. NaN is not valid in JSON. Maybe you should write a bug report. - Felix Kling

@cj333 Gist is now up to date and provides all those information needed. I wrapped all NaN occurences with "", so add an check for real info instead of NaN in your working application. hope that helps you :) - Samuel Herzog

@cj333 glad to help you :), i think it would be the smartest thing to replace it with null. if you're using my example you just have to change $NaNReplacement for that. Please also look for a way to fill an error report to MSNBC so they can fix the bug. - Samuel Herzog

@cj333: You could do str_replace(':NaN', ':null', $json) or str_replace(':NaN', ':""', $json). But there is always the possibility that the string contains :NaN at a legal position and changing it might change the data or even create another invalid string. You have to try. I would still file a bug report. - Felix Kling

First, what you are receiving is JSONP, not pure JSON. In order to descodificar the JSON, you have to remove, all(...); primero:

$data = trim($json, 'all();');

Second, you need to decode JSON. PHP does not do this automatically:

$data = json_decode($data);

Now you should be abel to do:

foreach($data->Documents as $result) {
    // something


Respondido el 09 de enero de 11 a las 01:01

@Felix Kling if he hadn't parsed $data he whould have got an error message with the first foreach and not with the htmlspecials fnc - Samuel Herzog

@Samuel Herzog: But he gets the error message: Advertencia: argumento no válido proporcionado para foreach (). You are right though, he is accessing the other values wrongly in the loop. - Felix Kling

@Felix Kling because the Docmultimedia Attribute is an array, that has nothing to do with decoding at all, also he already loops $data->Documents, i don't see how your answer would help - Samuel Herzog

@cj333: You might want to pass your JSON (without all(..);) A and see if it validates. - Felix Kling

@cj333: Not sure what you mean. JSONP is just JSON wrapped in a function call. The function call is all(...);. You have to remove this in order to get the JSON string. Have a look at Wikipedia for JSONP. - Felix Kling

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.