Parsing simple JSON using Ext gives SyntaxError: Unexpected token ILLEGAL - javascript

i'm doing a simple parse of some JSON and it's giving me an error in chrome, what am i missing here?
Ext.util.JSON.decode("{info: {synopsis: 'test'}}");
SyntaxError: Unexpected token ILLEGAL
Thanks a lot

http://www.json.org/
Think you should use double quotes instead of single quotes.
Ext.util.JSON.decode('{"info": {"synopsis": "test"}}');

Be careful if you are using ExtJs 4 onward, You have to use
Ext.JSON.decode('{"info": {"synopsis": "test"}}');

Related

why JSON.parse does not convert a string to json object?

I tried to convert a string to a javascript object using JSON.parse but it's not working.
I tried everything but did not get anything on the console.log and there is no error message.
JSON.parse(`{'exp': '1', 'input': '1d6404f66ed3d72e', 'iterate': 'no'}`);
Update
In the real code, I'm passing the value from an object
console.log(JSON.parse(future.onIOPub.data['text/plain']))
When you run this, you should see
Uncaught SyntaxError: Unexpected token ' in JSON at position 1
This error is because you are using single quotes. JSON only accepts double quotes, as described in the spec
https://www.json.org/json-en.html

Uncaught Syntax Error: Unexpected Identifier Value (include percent sign) Javascript

I am trying to assign the value to variable in simple Java Script but getting "unexpected identifier" error, I feel that the% signs are creating problem here.
Any suggestion please?
You need to surround strings with quotes (' or ")
the value you are assigning to the rid is string.
So, always use Quotes
'or"

JSON: Error while Parsing

I've the following JSON(Valid) sting.
[["abc","{\"icon\":\"adjust\",\"prefix\":\"fa\",\"markerColor\":\"red\"}"],["xyz","{\"icon\":\"archive\",\"prefix\":\"fa\",\"markerColor\":\"green\"}"],["azs","{\"icon\":\"asterisk\",\"prefix\":\"fa\",\"markerColor\":\"darkred\"}"]]
it gives error when I try to Parse using the JSON.parse function
here is the code that I'm using for parsing.
JSON.parse('[["abc","{\"icon\":\"adjust\",\"prefix\":\"fa\",\"markerColor\":\"red\"}"],["xyz","{\"icon\":\"archive\",\"prefix\":\"fa\",\"markerColor\":\"green\"}"],["azs","{\"icon\":\"asterisk\",\"prefix\":\"fa\",\"markerColor\":\"darkred\"}"]]');
and it gives an error in console Uncaught SyntaxError: Unexpected token i
here is the Correct Output by same string using online JSON viewer.
When you use JSON viewer, it's different from when you use the code in your JS code. Like #Jonathan stated, you should double escape you json sting.
JSON.parse('[["abc","{\\"icon\\":\\"adjust\\",\\"prefix\\":\\"fa\\",\\"markerColor\\":\\"red\\"}"],["xyz","{\\"icon\\":\\"archive\\",\\"prefix\\":\\"fa\\",\\"markerColor\\":\\"green\\"}"],["azs","{\\"icon\\":\\"asterisk\\",\\"prefix\\":\\"fa\\",\\"markerColor\\":\\"darkred\\"}"]]');
Your json structure is invalid. You should use this instead(without slashes):
'[["abc",["icon":"adjust","prefix":"fa","markerColor":"red"]],["xyz",["icon":"archive","prefix":"fa","markerColor":"green"]],["azs",["icon":"asterisk","prefix":"fa","markerColor":"darkred"]]'

Uncaught SyntaxError: Unexpected token i

a = JSON.parse('["<input class="image" type="file" name="%simg_%d"/>"]');
gives the above error. Can someone explain why? Is %s the start of some kind of special character string in json/javascript?
You don't escape the quotes so you keep breaking in and out of the string which makes for invalid JSON
JSON.parse('["<input class=\\"image\\" type=\\"file\\" name=\\"%simg_%d\\"/>"]');

Javascript Syntax error: Unexpected token ILLEGAL

When i use the javascript code ( http://jsfiddle.net/TRyhK/ ) :
var test = "#[]
#[]";
I reiceive a strange error: SyntaxError: Unexpected token ILLEGAL.
Does anyone know how to fix it?
Thanks
Line feeds in string literals can be expressed with \n:
var test = "#[]\n#[]";

Categories

Resources