I am using JSON.parse to parse a json string. It works fine for standard json format string but I doesn't work for some string like below:
'{"filter":{"email":/net/},
"rejected":"[/net/,/net/]"
}'
the problem on this string is that it missing double quote on /net/ which makes JSON.parse failed. Is there a way to parse this string into json object? Or is there a library to add a double quote on the missing part of this string? I tried a few regex patterns to make up this string but can't find a good solution.
Related
Hello I am wondering why this line doesn't work:
JSON.parse({"a":"\u00A9"})
I tried to serach in MDN website but I didn't find anything referring to in json.parse
Unicode escaping is syntactically legal in js according to this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#String_literals
What is the problem and how can I safely parse text with JSON.parse
{"a":"\u00A9"} is a JavaScript object literal.
JSON.parse expects to be passed a string so it is implicitly converted to a string ("[object Object]").
The [ is fine, because a JSON text can start with an array.
The o is then an error because it isn't allowed there.
A literal copyright symbol (remember that \u00A9 inside a JavaScript string literal will be consumed by the JS parser before it gets to the JSON parser) or the unicode escape sequence would be fine.
console.log(JSON.parse('{"a":"\u00A9"}'));
console.log(JSON.parse('{"a":"\\u00A9"}'));
Note that creating a string literal in JS source code that contains JSON and then parsing it is a terrible idea. You have to deal with nested levels of escaping, and it is inefficient.
If you have an object: use the object.
var data = {"a":"\u00A9"};
console.log(data.a);
I am having problem converting a JSON String into Javascript object.
I ran into a few suggestions which said that I should not be using multi-line string but using single line string too did not work.
Snippet: https://jsfiddle.net/ankschoubey/hjh2d3z6/
SyntaxError: Unexpected token F in JSON at position 4536
Because you're including JSON as a JavaScript string value, you'll have to double-up on all the embedded backslash characters because they'll be parsed twice: first when the JavaScript parser reads the overall string constant to create the string value, and then again when you call JSON.parse().
Thus that portion of the string with \" should be \\". That way, the JavaScript string parsing will turn \\" into just \", and that's what will make the JSON parser happy.
I am getting a string object after splitting from a text. Now I am trying to convert that text to a JSON object.
Text after Splitting
{location:"Web",initial:"",firmType:"",toaxfrtype:""}
When I am trying to parse it using JSON.parse, I am getting an error,
SyntaxError: Unexpected token l.
I have some other string value as the same in the above text. It was parsing fine using JSON.parse. Only the above string is not working.
Can anybody help me in this issue.
You need quotes " so change
{location:"Web",initial:"",firmType:"",toaxfrtype:""}
to
{"location":"Web","initial":"","firmType":"","toaxfrtype":""}
I have attached the image where it says valid now after correction. You can online validators to validate it first.
This is not JSON. You need to quote all Strings, including the keys.
I am using Dojo.fromJson to convert json string to javascript object, but throw exception. Because, there are control characters such as ',\n,\r in the json string.
How can I solve this problem in dojo? convert json string to javascript object, even if there are control characters.
I use Newtonsoft.JsonConvert.SerializeObject to convert C# oject to json data. Json Object: {"name":"'\"abc\n123\r"} then, I use Dojo.fromJson(' {"name":"'\"abc\n123\r"}') to convert json data to javascript object.
Thank you very much!
Problem, i believe is the double-quote which should be escaped by triple backslashes. You can use "native browser JSON decode" as searchterm for "dojo fromJson" synonym.
Without knowing my way around C# - I havent tested but i believe following should work:
string c_sharp_name = "'\"abc\n123\r";
// C#Object.name
c_sharp_name = c_sharp_name.
replace('"', '\\"'). // maybe add a slash on serverside
replace('\n', '\\\n').
replace('\r', '\\\r');
since
while this fails:
{"name":"'\"abc\n123\r"} // your single backslash
this would work:
{"name":"'\\\"abc\\\n123\\\r"} // working triple backslash escape
I got a json structure somehow as below and my question is how can i parse this with jQuery so that i can use it like myJson[0].name and than alert it so that "M\\xe9t\\xe9o" = Météo.
Jquery tells me this is invalid json why ?
Json uses double backslash if i use single backslash ("M\xe9t\xe9o") Jquery is OK with the syntax.
var jsonObj = '{"title":[{"id":"1","name": "M\\xe9t\\xe9o"},{"id":"2","name": "Meteo"}]}';
var myJson = jQuery.parseJSON(jsonObj);
The JSON syntax only allows \uxxxx escapes.
Change it to "M\\u00e9t\\u00e9o".
If you use a single backslash, it gets parsed by the Javascript string literal, so the actual string value contains the real Unicode character, not an escape. In other words, "M\xe9t\xe9o" === "Météo"
It is looks like the json was incorrectly (manually?) encoded. When you encode it in UTF-8, e.g. with PHP, you'll get:
{"title":[{"id":"1","name": "M\u00e9t\u00e9o"},{"id":"2","name": "Meteo"}]}
which is correctly parsed by JS. But \xe9 is unrecognized by parser.