How to access Json [duplicate] - javascript

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);

Related

Read nested value from json [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 3 years ago.
On the image You can see what I want to read
console.log("exact val = " + json._array.loftybot.spells.name)
and it gives [TypeError: Cannot read property 'spells' of undefined]
You can see in terminal structure of json
How to get into these values?
I want save values from json to array const json = new ObservableArray
And then use all of these values in my program
How about json._array[0].loftybot.spells.name? with an index 0 on your _array and go further on nested json. SEE at MDN

How to read Object? [duplicate]

This question already has answers here:
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Closed 5 years ago.
Say i have object like
var a = {"user":
{'average':
{'score':4
}
}
}
How can I read object value using its keys
Say I have user Object with me and have key "average.score" can I get the value directly?
a.user["average.score"];
//Coming as undefined
a.user["average"]["score"]
// Working as expected : 4
I have the key of "average.score" all together with me want to get the value of score how can I do it directly without splitting the key.
Use a.user["average"].score
var a = {"user":
{'average':
{'score':4
}
}
}
console.log(a.user["average"].score);

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

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

How to correctly get JSON value? [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 9 years ago.
I have this json array
var currencyformats =
{"USD":[
{'symbol':'$', 'left':true}
],
"UAH":[
{'symbol':'₴', 'left':true}
],
"EUR":[
{'symbol':'€', 'left':false}
]
};
How retreive '₴' ?
I tried this (in cookie "to" I've "UAH")
currencyformats[$.cookie("to")].symbol
but I've obtained undefined
The problem is that under each country code, you've defined an array with a single object. That means that after you access the country code, you'll need to also access the first index in the array.
So, assuming the cookie is set to the value that you're expecting:
currencyformats[$.cookie("to")][0].symbol;

Categories

Resources