Can not convert JSON List to Array in Javascript

0

I'm having trouble getting javascript data from a json.

To better explain, the data comes from the database, like this:

// PHP

$rs = mysqli_query($con, "SELECT id_acao as id, text FROM acoes");
$data = array();
if (mysqli_num_rows($rs) > 0) {
    while($row = mysqli_fetch_assoc($rs)){
        $data[] =  $row;            
    }
}
echo json_encode($data);

This is returning the following (as a string):

  bla bla bla "}, {" id ":" 9 "," text ":" bla bla bla "}]

That is, it returns a list and not a JsonString. In the page that receives I can not convert it to access the data ..

So:

var data = JSON.parse(ajax.responseText[0]);

or so (as I saw in some forums the use of [] .concat)

var data = JSON.parse(([].concat(ajax.responseText))[0]);

gives the same error.

  

Uncaught SyntaxError: Unexpected token in JSON at position 0 at JSON.parse ()

I was only able to convert it to an object by doing this:

var data =  [].concat(JSON.parse(JSON.stringify(ajax.responseText)));

But when you try to access it with

var valor = data[0];

or even with

var valor = data[0]['text'];

I'm returning a type (string) with all the content of ajax !!

What am I forgetting or doing wrong? Thank you to anyone who can help me! At

UPDATE: I was testing inserting the value directly into JSON.parse instead of using a variable.

Amazingly, if I type exactly what is in ajax.responseText  works. If you copy and paste the contents, through the console.log, it gives an error.

// typed, works fine

var data = JSON.parse('[{"id":"1","text":"Windows XP"},{"id":"9","text":"não concluído"}]');

// copied and pasted from console.log(ajax.responseText); , gives error

var data = JSON.parse('[{"id":"1","text":"Windows XP"},{"id":"9","text":"não concluído"}]');

I'm finding that some special invisible character is in the middle of the string of ajax.responseText .

I have tested both codes above, my last two quotes, and worked out both.

    
asked by anonymous 11.07.2017 / 15:58

2 answers

0

After much searching and testing, I found the problem!

My PHP codes were infected with an invisible character called " ZERO WIDTH NO-BREAK SPACE "

Even though I "settling" correct formatting in Notepad ++ it persisted.

I was able to solve with a PHP script that removes this invisible character in all my PHP files!

The same can be found in the gringo post here: link

I hope it helps others with the same problem!

At

    
11.07.2017 / 21:10
1

Based on the principle that responseText is exactly [{"id":"1","text":"bla bla bla"},{"id":"9","text":"bla bla bla"}] parse works fine:

var ajax = {
  responseText: '[{"id":"1","text":"bla bla bla"},{"id":"9","text":"bla bla bla"}]'
};
     
var data = JSON.parse(ajax.responseText);

console.log(data);
console.log(data[0].text);

If the request was made in JQuery and the data type has already been specified as dataType='json' then the response already comes in json and it is no longer possible to parse.

I also advise you to confirm with console.log the exact amount you are receiving in the response, in order to understand where the problem comes from.

    
11.07.2017 / 16:30