receive json_encode ( (array) $objj) in jquery

Asked

Viewed 475 times

0

I got the following return of PHP for JSON through AJAX:

[
  "ok",
  [
    [
      {
        "\u0000CelulasReunioes\u0000idCelula":"17",
        "\u0000CelulasReunioes\u0000data":"2019-01-30",
        "\u0000CelulasReunioes\u0000presentes":"0",
        "\u0000CelulasReunioes\u0000visitas":"0",
        "\u0000CelulasReunioes\u0000criancas":"0",
        "\u0000CelulasReunioes\u0000decisoes":"0",
        "\u0000CelulasReunioes\u0000discipulados":"0",
        "\u0000CelulasReunioes\u0000jejum":"s",
        "\u0000CelulasReunioes\u0000evangelismo":"s",
        "\u0000CelulasReunioes\u0000supervisao":"s",
        "\u0000CelulasReunioes\u0000oferta":"30"
      },
      {
        "\u0000CelulasReunioes\u0000idCelula":"17",
        "\u0000CelulasReunioes\u0000data":"2019-02-01",
        "\u0000CelulasReunioes\u0000presentes":"10",
        "\u0000CelulasReunioes\u0000visitas":"2",
        "\u0000CelulasReunioes\u0000criancas":"2",
        "\u0000CelulasReunioes\u0000decisoes":"6",
        "\u0000CelulasReunioes\u0000discipulados":"1",
        "\u0000CelulasReunioes\u0000jejum":"n",
        "\u0000CelulasReunioes\u0000evangelismo":"n",
        "\u0000CelulasReunioes\u0000supervisao":"n",
        "\u0000CelulasReunioes\u0000oferta":"0"
      }
   ]
 ]
]

I basically made a (array) $objeto in the PHP to use json_encode () for they are returns of PHP objects

Now, I intend to use that array of 2 positions`

retorno [ 0 ] = "ok";
retorno [ 1 ] = array PHP de objetos convertidos por (array) com vários índices;

Catch the position [ 1 ] of array and make a foreach.

resultado.forEach(function(reuniao, index){     

    html  = '<h2>' + reuniao.idCelula + '</h2>';
    html += '<ul class="vertical">';
    html += '  <li><label class="labelPequeno">Data</label> : ' + reuniao.data + '</li>';
    html += '</ul>';    

});

Obs.:

My purpose here is to transform the return:

"\u0000CelulasReunioes\u0000idCelula"

in

"CelulasReunioesidCelula"

and, if possible, preferably:

"idCelula"

Can someone help me?

  • Have you managed to do the ajax part? Where, here in the question, is some jQuery method, as you quote in the title? I didn’t quite understand "where you’re struggling"...

  • Yes: the part that is like this: "u0000Cellulars u0000idCellular":"17", is already the return of the ajax. Here it is normal. Now I need to transform that return that comes out on the console like this : Cellulasreunions idCellular, that in jQuery. Realize that u0000 means a blank space

  • CelulasReunioes is the name of your class in PHP? If so, how did you form this JSON? From what I understand, your problem lies in the formation of JSON. You better fix this in PHP, don’t you think? What do you think of [Edit] the question and post your PHP code, if applicable?

  • (array) $object. So I formed the PHP array, but it delivers something like this " u0000CelulasReunions u0000idCelula":"17". for the incus. It puts a space before and after the name of the Class

  • Does it serve you an answer that "corrects" your PHP? When I say correct, it means doing it another way which I think is more appropriate. Then you judge if it’s better for your application...

  • yes, but it would be by converting the object to an array attribute by attribute or there is a more direct function like what does the (array) $object°

  • My answer already explains and exemplifies it. I confess that I am still curious! How did you manage to get to a JSON with the class name on each attribute? I tried to imitate your JSON but could not! @_@

  • as I explained, I converted objects into array with the function (array) $object.

Show 4 more comments

1 answer

0

As proposed in the comments, I will reformulate your PHP (that was not posted, but let’s say, I developed that part from scratch).

<?php
class CelulasReunioes
{
    public $idCelula, $data, $presentes, $visitas,
        $criancas, $decisoes, $discipulados, $jejum,
        $evangelismo, $supervisao, $oferta;

    public function __construct($dados)
    {
        $this->idCelula = $dados[0];
        $this->data = $dados[1];
        $this->presentes = $dados[2];
        $this->visitas = $dados[3];
        $this->criancas = $dados[4];
        $this->decisoes = $dados[5];
        $this->discipulados = $dados[6];
        $this->jejum = $dados[7];
        $this->evangelismo = $dados[8];
        $this->supervisao = $dados[9];
        $this->oferta = $dados[10];
    }

}

$obj1 = new CelulasReunioes(
    array(
        '17', '2019-01-30', '0', '0', '0', '0', '0', 's', 's', 's', '30'
    )
);
$obj2 = new CelulasReunioes(
    array(
        '17', '2019-02-01', '10', '2', '2', '6', '1', 'n', 'n', 'n', '0'
    )
);

header('Content-Type: application/json');

$retorno = array('ok', array($obj1, $obj2));

echo json_encode($retorno, true);

It’s a simple class, just to simulate the formation of JSON... Ignore my class, just create your JSON as in my example.

Nominee json.php, this file returns the JSON to me as follows (formatted via Jsonlint.com):

["ok", [{
    "idCelula": "17",
    "data": "2019-01-30",
    "presentes": "0",
    "visitas": "0",
    "criancas": "0",
    "decisoes": "0",
    "discipulados": "0",
    "jejum": "s",
    "evangelismo": "s",
    "supervisao": "s",
    "oferta": "30"
}, {
    "idCelula": "17",
    "data": "2019-02-01",
    "presentes": "10",
    "visitas": "2",
    "criancas": "2",
    "decisoes": "6",
    "discipulados": "1",
    "jejum": "n",
    "evangelismo": "n",
    "supervisao": "n",
    "oferta": "0"
}]]

Now, in the client (HTML/Javascript):

$.ajax({
    url: 'http://localhost/json.php',
    dataType: 'json'
})
.done(function(resultado){
    resultado[1].forEach(function(reuniao, index){
        let html  = '<h2>' + reuniao.idCelula + '</h2>';
        html += '<ul class="vertical">';
        html += '  <li><label class="labelPequeno">Data</label> : ' + reuniao.data + '</li>';
        html += '</ul>';
        // Aqui o html já está pronto para o uso! Aplique-o onde quiser.
        console.log(html);
    });
});

Basically, I added the index 1 in the forEach in order to do the repetition only in the array containing the objects: resultado[1].forEach(...);.

At the end of the replay, I played a console.log(html) in order to test. Just replace that part of the code as your need.

If you want to see everything working, you can download in mine Github/Lipespry and simulate in their environment.

PS: I made the Ajax request using the jQuery version 3.3.1;

  • Yeah. I had already done that. But what I didn’t want was just that. Scroll through the object attribute by attribute. I would like to know if there is another native PHP function that, like (array) converts to array without having to go through the attributes

  • Colleague, the json_encode will already do all this service. I did not go through attribute by attribute. I just requested the class 2 times to be like yours! I don’t seem to understand your objective... xD

Browser other questions tagged

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