How to escape # in javascript to retrive object property [duplicate] - javascript

This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 7 years ago.
i have Javascript object as below.
How can i get value for SERIAL#, like i can retrieve *.INST_ID or *.INSTANCE.
how can i escape # and get the value required.
So far i have tried SERIAL#23% but none helped so far.
var t = {"INST_ID":"1","INSTANCE":"xina","SID":"27","SERIAL#":"48810", "PROGRAM":"Perl#app01"}
console.log(kSess.SERIAL%23); gives syntax error
I am parsing variable "t"
This data is coming from java code, so there is nothing much i can do to change SERIAL# to something else
Any idea?

Try to retrieve the value like this...
t["SERIAL#"]; // will return you the value..
Store it somewhere or play with it as you like. :)

Related

Access object key containing character : [duplicate]

This question already has an answer here:
Parsing a JSON object with Special characters in property key
(1 answer)
Closed 3 years ago.
I am pulling in reviews from iOS and the key they have has a : in the middle.
Like so: im:rating: {attributes: {…}}
Therefore, doing something like this is not working. {entry.im:rating.label}
How do I get the label from im:rating?
Simple answer: entry['im:rating'].label

Reading Json slash in Javascript [duplicate]

This question already has answers here:
How can I access object properties containing special characters?
(2 answers)
Closed 7 years ago.
I'm reading a JSON file through javascript. I'm having trouble getting today/_text because of the forward slash. I can get today successfully by doing: {{hours.results[0].today}}. How would I get today/_text? I've tried:
today\/_text
today/\_text
today//_text
today\\/_text
{"offset":0,"results":[{"today/_text":"Today:YES","today/_source":"/hours/1","today":"2,3,4"}]}
hours.results[0]["today/_text"] should do the trick!
hours.results[0] returns an object that has that as a key, making that the easiest way to access the property in question.

Jquery is not accepting hypen in the jsp page? [duplicate]

This question already has answers here:
How do I reference a JavaScript object property with a hyphen in it?
(11 answers)
Closed 8 years ago.
I have a field sent by SERVICE which is having hypen in it. Eg., first-name (As JSON object)
But when I try to get the value of that field through the jsp. I am getting a script error.
Please let me know how to access the hypen also in this?
var nameList = msg.RESPONSE.DATA.NAME-LIST;
The above way when I try to access it is throwing script error
A variable or property name with an hyphen is indeed wrong in javascript (Jquery).
However, you can access the "problematic" property like this :
var nameList = msg.RESPONSE.DATA["NAME-LIST"];
I would recommend to rename the property(ies)
without hyphen if you control the content of this response

Javascript replace not working with ampersand and defining a letter [duplicate]

This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 8 years ago.
I have written a JSFiddle with the expected output and output my code is currently doing. The two different values must be parsed as either a colon or a semi-colon as I need to know what one line to parse in php is.
var data = "key=update.repositories&value=xime+mcsg+mcsg-maps&key=server.minPlayersToStart&value=12";
data.replace(/&v/g, ":v");
data.replace(/&k/g, ";k");
$(".encData").text(data);
Fiddle found here: http://jsfiddle.net/RS6xC/1/
string.replace() doesn't change the original variable, it returns a new value.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
So you need to reassign the returned value of the replace() method to the original variable, such as like this:
var data = "key=update.repositories&value=xime+mcsg+mcsg-maps&key=server.minPlayersToStart&value=12";
data = data.replace(/&v/g, ":v");
data = data.replace(/&k/g, ";k");
$(".encData").text(data);
.replace returns a new string, it does not modify the existing string.
Use:
data=data.replace(/&v/g, ":v");
data=data.replace(/&k/g, ";k");

read JSON data received from the mysql [duplicate]

This question already has answers here:
How can I access object properties containing special characters?
(2 answers)
Closed 9 years ago.
I want to read data in the javascript which are received from the server in JSON format.
I've been using this a lot but now it seems that I hit the wall with this example:
JSON
{
"results": [
{
"MIN(jtable.HIT_VALUE)": "70.200000",
"AVG(jtable.HIT_VALUE)": "124.4077234969",
"MAX(jtable.HIT_VALUE)": "1854.620000"
}
]
}
JAVASCRIPT
How to read this values?
I have tried this
response.results[i].MIN(jtable.HIT_VALUE)
and I'm getting this error:
TypeError: Object #<Object> has no method 'MIN'
Any ideas?
MIN(jtable.HIT_VALUE) is the key and has to be used as such using the square bracket notation like
response.results[i]['MIN(jtable.HIT_VALUE)']
Use it as string:
response.results[i]['MIN(jtable.HIT_VALUE)']
JavaScript interprets the call response.results[i].MIN(jtable.HIT_VALUE) as an attempt to call a nonexistent function MIN.
Consider using this:
response.results[i]["MIN(jtable.HIT_VALUE)"]

Categories

Resources