This question already has answers here:
Parsing string as JSON with single quotes?
(10 answers)
Closed 3 years ago.
I am experiencing a very strange behavior of Javascript.
I get the data object in the form of a string from the server as shown below,
"{'id':1234, 'name'}"
When I try to parse this data using JSON.parse() it throws
JSON.parse: expected property name or '}' at line 1 column 2 of the JSON data
However changing the data to '{"id":1234, "name"}' will work.
But my question is how do I transform:
"{'id':1234, 'name'}" to '{"id":1234, "name"}'
in the javascript end? (I dont want to change any thing in the server).
Simply you need to replace the character(') as global, here the code:
var yourString = "{'id':1234, 'name'}";
yourString = yourString.replace(/\'/g, '"');
console.log(yourString);
And in vice versa:
var yourString = '"{\'id\':1234, \'name\'}"';
yourString = yourString.replace(/\'/g, '"');
yourString = yourString.replace(/\"/g, "'");
console.log(yourString);
Here other example with mixed characters( " and ' ):
var yourString = "{\"id':1234, 'name'}";
yourString = yourString.replace(/\'/g, '"');
console.log(yourString); // Automatically skips the right character(")
Related
This question already has answers here:
Converting a string to JSON object
(10 answers)
Closed 6 years ago.
I'm trying to parse this string into a JSON:
"{'firstname':'Jesper','surname':'Aaberg','phone':'555-0100'}"
I'm doing it like so:
var strJSON = "{'firstname':'Jesper','surname':'Aaberg','phone':'555-0100'}";
console.log(JSON.parse(strJSON));
But I get the error message:
VM652:1 Uncaught SyntaxError: Unexpected token ' in JSON at position 1
at JSON.parse ()
Does anybody know what am I missing and how can i solve it?
You can replace single quotes to double quotes and parse it.
var str = "{'firstname':'Jesper','surname':'Aaberg','phone':'555-0100'}";
var o = JSON.parse(str.replace(/\'/g, "\""));
console.log(o)
Single quotes are not valid for strings, you need to use double quotes instead:
var strJSON = '{"firstname":"Jesper","surname":"Aaberg","phone":"555-0100"}';
Just change your string:
"{'firstname':'Jesper','surname':'Aaberg','phone':'555-0100'}"
to:
'{"firstname":"Jesper","surname":"Aaberg","phone":"555-0100"}'
JSON only supports double-quotes
var str = '{"firstname":"Jesper","surname":"Aaberg","phone":"555-0100"}';
console.log(JSON.parse(str));
Use this.
This question already has answers here:
Javascript, Razor and Escape characters. Like apostrophe
(5 answers)
Closed 7 years ago.
I'm assigning the value of JS string variable from server side code.
Like this
var fbShareTitle = "#(ViewBag.LeadTitle as string)";
ViewBag return string value is
A "Fantastic" Lead With 'Qoute'
Now It is giving error in console
SyntaxError: missing ; before statement
I have tried this
var fbShareTitle = ("#(ViewBag.LeadTitle)").replace(/"/g, '\\"');
But now I'm getting this error.
SyntaxError: missing ; before statement
As This string will be shared on fb, So i can't modify string, like replace all " with ' e.t.c.
The reason why your code doesn't work is that Razor will generate the following:
var fbShareTitle = "A "Fantastic" Lead With 'Qoute'";
which is invalid JavaScript. You can't simply fix it by replace, since it's not the problem that your string is bad, it's that your code can't parse - replace never gets to execute. You need to fix it on serverside, where you generate the JavaScript in question, by modifying your Razor code:
var fbShareTitle = #Html.Raw(Json.Encode(ViewBag.LeadTitle as string));
Json will take care of quotes and proper escaping; Raw will make sure you don't get your < and > replaced. Extra benefit from #Html.Raw(Json.Encode(...)) mantra: you can use it to inject any kind of data that can be encoded in JSON, not only strings.
This question already has answers here:
How can I accommodate a string with both single and double quotes inside of it in JavaScript
(4 answers)
How to escape special characters in building a JSON string?
(10 answers)
Closed 7 years ago.
Javascript fails to read this json string as it contains a single quote character which it sees as the end of the string.
How can I escape the single quote so that it is not seen as the end of the string?
var json = '{"1440167924916":{"id":1440167924916,"type":"text","content":"It's a test!"}}';
var parsed = JSON.parse(json);
Use a backslash to escape the character:
var json = '{"1440167924916":{"id":1440167924916,"type":"text","content":"It\'s a test!"}}';
var parsed = JSON.parse(json);
Just escape the single quote with a backslash such as \':
var json = '{"1440167924916":{"id":1440167924916,"type":"text","content":"It\'s a test!"}}';
var parsed = JSON.parse(json);
//Output parsed to the document using JSON.stringify so it's human-readable and not just "[object Object]":
document.write(JSON.stringify(parsed));
Escape it with a backslash
var json = '{"1440167924916":{"id":1440167924916,"type":"text","content":"It\'s a test!"}}';
var parsed = JSON.parse(json);
This question already has answers here:
How to access object property with invalid characters
(2 answers)
Closed 7 years ago.
My JSON structure is as follows
"{"Key":{"#text":"100150410150347261963/output/Five String.mp4"},"LastModified":{"#text":"2015-05-26T15:33:39.000Z"},"ETag":{"#text":"\"5e5fd36802186f81109a9adedcb802fe\""},"Size":{"#text":"18831126"},"StorageClass":{"#text":"STANDARD"}}"
This is my code
var data = JSON.parse("{"Key":{"#text":"100150410150347261963/output/Five String.mp4"},"LastModified":{"#text":"2015-05-26T15:33:39.000Z"},"ETag":{"#text":"\"5e5fd36802186f81109a9adedcb802fe\""},"Size":{"#text":"18831126"},"StorageClass":{"#text":"STANDARD"}}");
var key = data.Key;
Now I want to read the value '100150410150347261963/output/Five String.mp4' but the key to this value is '#text', which starts with a # character. How can I read this?
var value = key.#text;
or
var value = key.'#text';
is not working. Is there any way to read this value?
PS: Please ignore the escaping of double quote '"' characters
Use bracket notation:
var value = data.Key['#text'];
This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 4 years ago.
I have a simple string that I'm trying to manipulate:
Your order will be processed soon:
I grab the string using:
var html = jQuery('.checkout td h4').html();
I then try to replace the ':' using:
html.replace(":", ".");
When I print it out to the console, the string is the same as the original string. I've also tried making sure that the html variable is of type "string" by doing the following:
html = html + "";
That doesn't do anything. In searching around, it seems that the replace function does a RegEx search and that the ":" character might have a special meaning. I do not know how to fix this. Can someone help me get rid of this stinkin' colon?
Slightly related...
I couldn't get these answers to work to replace all ":" in a string for the url encoded character %3a and modified this answer by'xdazz' to work: Javascript: Replace colon and comma characters to get...
str = str.replace(/:\s*/g, "%3a");
In your case it would be
str = str.replace(/:\s*/g, ".");
If you wanted to replace all colons with periods on a longer string.
Hope this helps somebody else.
The replace function returns a new string with the replacements made.
Javascript strings are immutable—it cannot modify the original string.
You need to write html = html.replace(":", ".");
I think c++ is the only high level language where strings are mutable. This means that replace cannot modify the string it operates on and so must return a new string instead.
Try the following instead
var element = jQuery('.checkout td h4');
element.html(element.html().replace(":", "."));
Or, perhaps more correctly (since you may have multiple elements).
jQuery('.checkout td h4').html(
function (index, oldHtml) {
return oldHtml.replace(":", ".");
}
);