read JSON data received from the mysql [duplicate] - javascript

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)"]

Related

issue with output of json file caused by blank in the javascript [duplicate]

This question already has answers here:
How can I access a JavaScript object which has spaces in the object's key?
(4 answers)
Closed 6 years ago.
I need to get some data out of a json file. The issue I have in the output there is a blank in the column header. I already tried to replace the blenk with a "_" but this did not help. When I simply name it as it is I am getting the error:
SyntaxError: Expected token '}' file:
Can you tell me how I need to manipulate the script to get the Data?
{
tableData.push({
"id" : id_temp,
"datetime": feat[j].datetime,
"Percent_Available_Memory": feat[j].Percent Available Memory,
"Available_Memory": feat[j].Available_Memory,
"Total_Memory": feat[j].Total Memory,
"coverage": feat[j].coverage
});
}
OK, try this:
"Percent_Available_Memory": feat[j]["Percent Available Memory"],
...
"Total_Memory": feat[j]["Total Memory"],

How to access Json [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 6 years ago.
I am trying to read from a JSON array with a nested array which has its value name spaced out. So I get an error whenever I run the code.
var error = [
{
"LessonName":"Understanding Multiplication",
"LessonID":"13343",
"no of questions":[{"Locked":"31","Unlocked":5}]
},
{
"LessonName":"Finding Unknown Values ",
"LessonID":"13424",
"no of questions":[{"Locked":"34","Unlocked":5}]
}
]
function jsd(){
document.write(error[0].LessonName);
document.write(error[0].'no of questions'[0].Locked);
}
document.write(error[0]."no of questions"[0].Locked); Doesn't seem to display.
You may use a property accessor with brackets for the string.
error[0]['no of questions'][0].Locked
You must use this syntax for strings with spaces.
document.write(error[0]['no of questions'][0].Locked);

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

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. :)

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.

Javascript, cannot access JSON property [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 8 years ago.
I have a json string received from websocket :
{
"type":"newLoan",
"cartItems":{
"numberOfItems":null,
"bookList":[
{
"id":"1",
"title":"The Count of Monte Cristo",
"author":"Alexandre Dumas ",
"genre":"Comedy",
"returnDate":"January 15"
}
]
}
}
This result is shown after console.log(receivedMessage), but when I try to access type property by console.log(receivedMessage["type"]) it gives me undefined.
Still the same with console.log(receivedMessage.type).
How could I access the type property?
There is probably an issue with the headers that are being sent along with your string, if the content type isn't set to application/json or the javascript equivalent then it will be treated as a string and no a json object
try:
JSON.parse(receivedMessage).type

Categories

Resources