Unable to convert JSON List to Array in Javascript

Asked

Viewed 377 times

0

I’m having trouble getting the javascript data from a json.

To better explain, the data comes from the database, thus:

//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 me the following (as a string) :

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

That is, it returns a list and not a Jsonstring. On the page you receive I cannot convert it to access the data..

Thus:

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

or so (as I have seen in some forums the use of []. Concat)

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

makes the same mistake.

Uncaught Syntaxerror: Unexpected token in JSON at position 0 at JSON.parse (<Anonymous>)

All I could do was convert it to an object:

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

But when trying to access it with

var valor = data[0];

or even with

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

is returning me a type (string) with all ajax content!!

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

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

Oddly enough, if I type in exactly what’s in ajax.responseText works. If I copy and paste the content, by the console.log, gives error.

// typed , it works

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

// copied and pasted from console.log(ajax.responseText);, makes a mistake

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

I’m thinking it’s some special, invisible character that’s in the middle of the string ajax.responseText.

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

  • What’s in ajax.responseText only?

  • As above, ajax.responseText there is a type (string) with all content, exactly the same as my first quote above in yellow.

  • The request was made from Jquery or normal javascript?

  • @Thiagoyb then his problem is precisely in JSON.parse(ajax.responseText[0]); that should only be JSON.parse(ajax.responseText);

  • So when do I var data = JSON.parse(ajax.responseText), error occurs exactly as above.

2 answers

1

Assuming that the responseText is exactly [{"id":"1","text":"bla bla bla"},{"id":"9","text":"bla bla bla"}] the parse works well:

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 answer already comes in json and is no longer possible nor necessary to parse.

I also advise you to confirm through console.log the exact amount you’re receiving in the answer, so you can see where the problem comes from.

  • When I do var data = JSON.parse(ajax.responseText), error occurs exactly as above. Already when I put the string [{..},{...}] with oacima, copying and pasting the right !! - I can access with data[0]['text']

  • console.log(ajax); presents what?

0


After a lot of research and testing, I found the problem!

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

Even though I "settando" correct formatting on Notepad++ it persisted.

I managed to resolve with a PHP script that removes this invisible character in all my PHP files!

The same can be found in the gringo post here: https://stackoverflow.com/a/9773452

I hope you help others with the same problem!

At

Browser other questions tagged

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