Json.parse does not convert to array

0

Hello, I am a full-fledged amateur in php and javascript and after a month of fighting with two languages that I had never stirred I got here.

I'm trying to pass a JSON from a request back to array in my javascript, but when I use parse and print in the console the typeof variable it continues as string and not as array. I will leave below the codes and what appeared in the console, along with more information.

PHP code:

<?php

    include("conexao_postgres.php");

    $ano_base = $_POST["ano_ref"];

    $sql = "SELECT * FROM public.parametros_estaduais WHERE ano_base = $ano_base";
    $resultado = pg_query($dbconn, $sql);
    $resultado_array = pg_fetch_row($resultado);

    echo json_encode($resultado_array, JSON_NUMERIC_CHECK);
?>

To streamline and facilitate, I've replicated the query with the test I'm doing in the main code to find out what's happening in PHP faster. What comes from the echo is this from here:

JSON > > [2016,0.02,347.58,528.51,902.93,1574.08,1745,880,396.49,14,152.84,207,0,35,20]

In my last question they asked me this, so now, pg_query returns a query resourse to DB and pg_fetch_row (I know it has the method that links to table column names, but then I would work with an obj I prefer to work with the array q is more direct, and yes, I already tested using pg_fetch_assoc and it n converts to an obj, continues as string also and not the error in JSON.parse in neither) returns a query row of resourse as an array.

Request code snippet:

$.post("query.php", {ano_ref: ano_base},
    function(retorno){
        var resposta = JSON.stringify(retorno);
        console.log("Data retorno: " + retorno + "Data resposta: " + resposta + "É uma string? " + typeof resposta);
        var vetor = JSON.parse(resposta);
        console.log("Data retorno: " + retorno + "Data vetor: " + vetor + "É um array? " + typeof vetor);
});

The console results:

Iknowit'samess,butthat'swhatI'vebeendoingsofar.Therearesome\n\tand\sdpsthatrunsstringfysoIhavenoideawhereitleftoff.Iimaginethatmaybethat'swherethemistakeis.Anotherdetail,Itriedtoconvertthestringthatcomesfromtherequestinthehand,puttingitinastringanddoingthesameprocess,anditworks,whichleadsmetoconcludethatthenextJSONformathasnoproblems,Icopiedandpasteditdirectlyofthepage(Ihavethephpmsminthemaincode,asIsaidtomakeiteasierformetoseewhatwashappening).

Suggestionforcomments*:

<?phpinclude("conexao_postgres.php");

    $sql = "SELECT * FROM public.parametros_estaduais WHERE ano_base = 2016";
    $resultado = pg_query($dbconn, $sql);
    $resultado_array = pg_fetch_row($resultado);

    //echo json_encode($resultado_array, JSON_NUMERIC_CHECK | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_PRESERVE_ZERO_FRACTION | JSON_UNESCAPED_UNICODE);
    $json = array();
    for($i=0; $i < count($resultado_array); $i++){
        $json[$i] = $resultado_array[$i];
    }
    echo(json_encode($json, JSON_NUMERIC_CHECK));
?>
    
asked by anonymous 26.04.2017 / 14:21

1 answer

1

Problem solved as follows:

$.post("query.php", {ano_ref: ano_base},
    function(retorno){
        // Decodificando JSON para object/array
        var dados = eval('(' + retorno.toString() + ')');
        tratarDados(dados);
});
    
27.04.2017 / 16:15