-1

I've just started to learn php, javascript and other languages, also not native English speaker, so sorry for bad grammar in advance.

First of all I created a .json file from a query result using php:

while ($row = pg_fetch_array($result)) {
    $torre = $row['torre'];
    $janeiro = $row['janeiro'];
    $fevereiro = $row['fevereiro'];
    $marco = $row['marco'];
    $abril = $row['abril'];
    $maio = $row['maio'];
    $junho = $row['junho'];
    $julho = $row['julho'];
    $agosto = $row['agosto'];
    $setembro = $row['setembro'];
    $outubro = $row['outubro'];
    $novembro = $row['novembro'];
    $dezembro = $row['dezembro'];
    $faturamento[] = array('torre' => $torre,
        'janeiro' => $janeiro,
        'fevereiro' => $fevereiro,
        'marco' => $marco,
        'abril' => $abril,
        'maio' => $maio,
        'junho' => $junho,
        'julho' => $julho,
        'agosto' => $agosto,
        'setembro' => $setembro,
        'outubto' => $outubro,
        'novembro' => $novembro,
        'dezembro' => $dezembro
    );
}

    $response['faturamento'] = $faturamento;
    $fp = fopen('./results.json', 'w+');
    fwrite($fp, json_encode($response));
    fclose($fp);
    pg_close($conn);
?>

The .json reads fine, everything is as it should be:

     {
          "faturamento": [
            {
              "torre": "Database Admin",
              "janeiro": "R$ 0.00",
              "fevereiro": "R$ 0.00",
              "marco": "R$ 0.00",
              "abril": "R$ 0.00",
              "maio": "R$ 0.00",
              "junho": "R$ 0.00",
              "julho": "R$ 0.00",
              "agosto": "R$ 0.00",
              "setembro": "R$ 0.00",
              "outubto": "R$ 0.00",
              "novembro": "R$ 0.00",
              "dezembro": "R$ 0.00"
            }, 
    .
    .
    .
 ]
}

I'm having trouble with the parsing of the .json itself

   <script>
            var obj;
            obj = JSON.parse(JSON.stringify($.getJSON("./results.json")));
            console.log(obj);
   </script>

Basically I need to make this into an js object or array, wahtever works to generate a table (so i can export it to pdf later) and use it on a graph (jpgraphs or fusioncharts).

I have tried multiple ways, but always get "undefined", just the default "object" console output or an unexpected character at column 1 row 1 of the json, sometimes col 2 row 2. I have currently jquery-3.2.1 running.

  • 1
    Break it up a bit to see where the breakdown is, instead of running everything all together one one line. Only after you know it works should you pass functions into each other. And sometimes, not even then, if you want to do error checking. – aynber Oct 19 '17 at 19:12
  • 1
    You need to read the documentation for `getJSON`. That isn't how you use it. – Quentin Oct 19 '17 at 19:14

1 Answers1

1

If you are returning JSON from the PHP page, you just need:

$.getJSON("./results.json", function (data) {
    obj = data;
});
Fenton
  • 241,084
  • 71
  • 387
  • 401