How to turn JSON_ENCODE from PHP to JSON in javascript

Asked

Viewed 215 times

-2

Hello,

My javascript pulls the result of a select made in PHP, what arrives in Javascript is as follows:

inserir a descrição da imagem aqui

I do not understand why you are coming with this "string()" in front,

I’ve tried using JSON.parse, but it doesn’t work, as in the image below,

inserir a descrição da imagem aqui

The question is: How do I turn this string into JSON?

  • Welcome Sydrack Santos, for good answers this post will help you: https://pt.meta.stackoverflow.com/questions/5483/manual-de-como-n%C3%83o-fazer-perguntas/5485#5485

  • Can you show the code in PHP that generates this string and how are you receiving in Javascript? without seeing the code is difficult to help...

2 answers

0


You’re probably doing this in your PHP code:

var_dump($variavel);

What you need to do is:

echo $variavel;

0

In the context of your php

$suaVariavel = json_encode($seu_Obj_ou_Arr);
print($suaVariavel);

In the context of Javascript

$.getJSON(url.php, parans, function(respJson){
   // Aqui você acessa seu json já no formato de objeto.
   // Coloca isso --- respJson.nome --- em qualquer lugar que desejar.
   // E assim por diante.
});

PS: Don’t forget to char the jQuery.js on your page to run getJSON

Browser other questions tagged

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