I've been trying to store the data from a JSON file into a variable in Javascript and everything were good until the JSON.parse function didn't work.
My file is called test.json and it looks like this:
{
test: 'Hello World!'
}
I also tried changing it to:
{ test: 'Hello World' }
My javascript code is this:
var a = OwNet.get( 'core/config/test.json', function( Res ) {
// Res is the response from an AJAX request (where i requested the test.json file)
if( Res !== "" && Res !== undefined && Res !== null ) {
// Here i tried to replace the line breaks, carriage returns and spaces. It failed.
// (I also tried to remove it)
Res.replace( /\r\n|\r|\n|\s*/gm, "" );
// Here i tried to transform Res in an object
tmp = JSON.stringify( Res );
return JSON.parse( tmp ); // This returns a string instead an object
} else return null;
});
The only problem is that the variable 'a' isn't an object, instead it is a string, i've been looking for the answer but i haven't could.
stringify? You're trying to de-serialize, aren't you?JSON.stringifywill remove all line breaks and convert your JSON into a string.