Problems with json and php listing

Asked

Viewed 26 times

0

I have a line of json code that comes from a variable and I want to list this json list to enter a database but I can’t read it.

$datesSite = file_get_contents('site');

$Puxa_titulo = explode('<div class="gallery-content item-gallery__wrapper" data-gallery-id="default" data-full-images="[' , $dadosSite );
$Titulo = explode(']">' , $Puxa_titulo[1] );

 $json = $Titulo[0];

 echo $json;

the site returns it:

{"src": "foto1.jpg", "w": "1200", " h ":" 900 "}, {" src ":" foto2.jpg "," w ":" 1200 "," h ":" 900 "}, {" src ":" foto3.jpg "," w ":" 1200 "," h ":" 900 "}, {" src ":" foto5.jpg "," w ":" 1200 "," h ":" 900 "}

I need to read this json list he tried to do this but it didn’t work

for($i = 0; $i < count($json); $i++) {
    echo "<div>SRC: " . $json[$i]->{'src'} . "</div>";
    echo "<div>w: " . $json[$i]->{'w'} . "</div>";
    echo "<br />";
}
  • Have you already performed json_decode? To turn json into a possible object to use php.

  • I don’t know how to do this

  • puts exactly what $json returns.

  • {"src": "foto1.jpg", "w": "1200", "h ":" 900 "}, {" src ":" foto2.jpg"," w ":" 1200"," h ":" 900 "}, {" src ":" foto3.jpg"," w ":" 1200"," h ":" 900 "}, {" src ":" foto5.jpg"," w ":" 1200"," h ":" 900 "}

  • I posted an answer, try it there.

1 answer

1


Put it like this:

$Puxa_titulo = explode('<div class="gallery-content item-gallery__wrapper" data- 
gallery-id="default" data-full-images="[' , $dadosSite );
$Titulo = explode(']">' , $Puxa_titulo[1] );

$json = $Titulo[0];
$json = json_decode(html_entity_decode($json));

And change the loop to the following shape:

for($i = 0; $i < count($json); $i++) {
  echo "<div>SRC: " . $json[$i]->src . "</div>";
  echo "<div>w: " . $json[$i]->w . "</div>";
  echo "<br />";
}
  • he lists nothing

  • Try again the way I put the loop now.

  • continues to return nothing

  • Use print_r to check how $json looks after json_decode, so we can understand the problem

  • continues to return nothing

  • Are you returning any errors? Or are you able to visualize something after inspecting the elements?

  • nothing gets all blank

  • I solved it. the solution was to put html_entity_decode in $json, the quotes were turning &quot so json n could convert.

  • $json = json_decode(html_entity_decode($json));

  • Great Leandro, I edited the answer, if possible put the answer as correct to end this question.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.