Why I can't access json properties with dot (.) operator while passing a var through $.getjson()2019 Community Moderator ElectionWhy does Google prepend while(1); to their JSON responses?Mustache.js - accepts inputs as vars, but not as ajax gets: How to import or troubleshootJavaScript property access: dot notation vs. brackets?Odd results when testing boolean variable within $.getJSON functionGetting undefined when trying to acces a JSON itemAjax Pass Json to PHPFinding Item in JSON Array and adding it to another Array. JavascriptImpossible to get JSON from Service into angularJs RoutingPulling values from JSON using jQueryI am unable to render components dynamically in react.js,
Aliens englobed the Solar System: will we notice?
Look through the portal of every day
Make a transparent 448*448 image
Rejected in 4th interview round citing insufficient years of experience
Can you reject a postdoc offer after the PI has paid a large sum for flights/accommodation for your visit?
The bar has been raised
Replacing Windows 7 security updates with anti-virus?
How do I express some one as a black person?
How strictly should I take "Candidates must be local"?
In the late 1940’s to early 1950’s what technology was available that could melt a LOT of ice?
How did Alan Turing break the enigma code using the hint given by the lady in the bar?
Why is Beresheet doing a only a one-way trip?
Why doesn't this Google Translate ad use the word "Translation" instead of "Translate"?
Is "history" a male-biased word ("his+story")?
What wound would be of little consequence to a biped but terrible for a quadruped?
How to pass a string to a command that expects a file?
Do Bugbears' arms literally get longer when it's their turn?
Word for a person who has no opinion about whether god exists
Low budget alien movie about the Earth being cooked
"One can do his homework in the library"
A three room house but a three headED dog
How could our ancestors have domesticated a solitary predator?
Single word request: Harming the benefactor
Can't find the Shader/UVs tab
Why I can't access json properties with dot (.) operator while passing a var through $.getjson()
2019 Community Moderator ElectionWhy does Google prepend while(1); to their JSON responses?Mustache.js - accepts inputs as vars, but not as ajax gets: How to import or troubleshootJavaScript property access: dot notation vs. brackets?Odd results when testing boolean variable within $.getJSON functionGetting undefined when trying to acces a JSON itemAjax Pass Json to PHPFinding Item in JSON Array and adding it to another Array. JavascriptImpossible to get JSON from Service into angularJs RoutingPulling values from JSON using jQueryI am unable to render components dynamically in react.js,
I have a $getJSON() that i want to pass a var through how ever i always get undefined in console log.
I have done this before i just cant seem to figure out why this isnt working.
Any help would be appreciated.
here is my example
"harry":
"example": "test",
"example1": "test",
"james":
"example": "test",
"example1": "test",
"ben":
"example": "test",
"example1": "test",
var usersidno = $("#hiddenid").html();
$.getJSON(
"http://localhost/example/file.json",
function(json)
console.log(json.usersidno);
);
<div id="hiddenid">harry</div>
javascript jquery json
|
show 1 more comment
I have a $getJSON() that i want to pass a var through how ever i always get undefined in console log.
I have done this before i just cant seem to figure out why this isnt working.
Any help would be appreciated.
here is my example
"harry":
"example": "test",
"example1": "test",
"james":
"example": "test",
"example1": "test",
"ben":
"example": "test",
"example1": "test",
var usersidno = $("#hiddenid").html();
$.getJSON(
"http://localhost/example/file.json",
function(json)
console.log(json.usersidno);
);
<div id="hiddenid">harry</div>
javascript jquery json
1
in which variable you getundefined
?
– Bear Nithi
Mar 7 at 7:02
can you try logging 'json' object that you are receiving inside the function and see if there exists a property inside json with name useridno? as it seems there's no property named useridno inside the object.
– Zeeshan Adil
Mar 7 at 7:03
Shouldn't Harry, James, and Ben have comas between them?
– Chipster
Mar 7 at 7:11
if i console.log(json) i get all the name, as normal.
– Mic
Mar 7 at 7:14
2
Looks like what you're trying to do here isjson[usersidno]
, notjson.usersidno
. You want to get the property whose name matches the value inusersidno
, not the property namedusersidno
.
– JLRishe
Mar 7 at 7:15
|
show 1 more comment
I have a $getJSON() that i want to pass a var through how ever i always get undefined in console log.
I have done this before i just cant seem to figure out why this isnt working.
Any help would be appreciated.
here is my example
"harry":
"example": "test",
"example1": "test",
"james":
"example": "test",
"example1": "test",
"ben":
"example": "test",
"example1": "test",
var usersidno = $("#hiddenid").html();
$.getJSON(
"http://localhost/example/file.json",
function(json)
console.log(json.usersidno);
);
<div id="hiddenid">harry</div>
javascript jquery json
I have a $getJSON() that i want to pass a var through how ever i always get undefined in console log.
I have done this before i just cant seem to figure out why this isnt working.
Any help would be appreciated.
here is my example
"harry":
"example": "test",
"example1": "test",
"james":
"example": "test",
"example1": "test",
"ben":
"example": "test",
"example1": "test",
var usersidno = $("#hiddenid").html();
$.getJSON(
"http://localhost/example/file.json",
function(json)
console.log(json.usersidno);
);
<div id="hiddenid">harry</div>
"harry":
"example": "test",
"example1": "test",
"james":
"example": "test",
"example1": "test",
"ben":
"example": "test",
"example1": "test",
"harry":
"example": "test",
"example1": "test",
"james":
"example": "test",
"example1": "test",
"ben":
"example": "test",
"example1": "test",
var usersidno = $("#hiddenid").html();
$.getJSON(
"http://localhost/example/file.json",
function(json)
console.log(json.usersidno);
);
<div id="hiddenid">harry</div>
var usersidno = $("#hiddenid").html();
$.getJSON(
"http://localhost/example/file.json",
function(json)
console.log(json.usersidno);
);
<div id="hiddenid">harry</div>
javascript jquery json
javascript jquery json
edited Mar 7 at 8:43
Shubh Dixit
1
1
asked Mar 7 at 7:01
MicMic
648
648
1
in which variable you getundefined
?
– Bear Nithi
Mar 7 at 7:02
can you try logging 'json' object that you are receiving inside the function and see if there exists a property inside json with name useridno? as it seems there's no property named useridno inside the object.
– Zeeshan Adil
Mar 7 at 7:03
Shouldn't Harry, James, and Ben have comas between them?
– Chipster
Mar 7 at 7:11
if i console.log(json) i get all the name, as normal.
– Mic
Mar 7 at 7:14
2
Looks like what you're trying to do here isjson[usersidno]
, notjson.usersidno
. You want to get the property whose name matches the value inusersidno
, not the property namedusersidno
.
– JLRishe
Mar 7 at 7:15
|
show 1 more comment
1
in which variable you getundefined
?
– Bear Nithi
Mar 7 at 7:02
can you try logging 'json' object that you are receiving inside the function and see if there exists a property inside json with name useridno? as it seems there's no property named useridno inside the object.
– Zeeshan Adil
Mar 7 at 7:03
Shouldn't Harry, James, and Ben have comas between them?
– Chipster
Mar 7 at 7:11
if i console.log(json) i get all the name, as normal.
– Mic
Mar 7 at 7:14
2
Looks like what you're trying to do here isjson[usersidno]
, notjson.usersidno
. You want to get the property whose name matches the value inusersidno
, not the property namedusersidno
.
– JLRishe
Mar 7 at 7:15
1
1
in which variable you get
undefined
?– Bear Nithi
Mar 7 at 7:02
in which variable you get
undefined
?– Bear Nithi
Mar 7 at 7:02
can you try logging 'json' object that you are receiving inside the function and see if there exists a property inside json with name useridno? as it seems there's no property named useridno inside the object.
– Zeeshan Adil
Mar 7 at 7:03
can you try logging 'json' object that you are receiving inside the function and see if there exists a property inside json with name useridno? as it seems there's no property named useridno inside the object.
– Zeeshan Adil
Mar 7 at 7:03
Shouldn't Harry, James, and Ben have comas between them?
– Chipster
Mar 7 at 7:11
Shouldn't Harry, James, and Ben have comas between them?
– Chipster
Mar 7 at 7:11
if i console.log(json) i get all the name, as normal.
– Mic
Mar 7 at 7:14
if i console.log(json) i get all the name, as normal.
– Mic
Mar 7 at 7:14
2
2
Looks like what you're trying to do here is
json[usersidno]
, not json.usersidno
. You want to get the property whose name matches the value in usersidno
, not the property named usersidno
.– JLRishe
Mar 7 at 7:15
Looks like what you're trying to do here is
json[usersidno]
, not json.usersidno
. You want to get the property whose name matches the value in usersidno
, not the property named usersidno
.– JLRishe
Mar 7 at 7:15
|
show 1 more comment
2 Answers
2
active
oldest
votes
When you want to access an object's (JSON) property dynamically (using a variable or expression), you should use square bracket notation.
[your expression or variable inside square brackets]
. i.e, json[useridno]
var usersidno = $("#hiddenid").html();
$.getJSON(
"http://localhost/example/file.json",
function(json)
console.log(json[usersidno]); // try to use square brackets
);
add a comment |
This is a working example of ,your issue I have used an api ,that gives back a json ,You can use [] annotatiton to access object properties.
$(document).ready(function()
var user = $("#hiddenid").html()
console.log(typeof user)
$.getJSON(
"https://jsonplaceholder.typicode.com/todos/1",
function(json)
let js=JSON.parse(JSON.stringify(json))
console.log(js[user]);
console.log(js.user);//undefined
console.log(js)
);
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="hiddenid">userId</div>
Explanation:
var userid = 'userId';
console.log(x.userid); // it looks for x.userid, which throws undefined,if it does'nt exists
console.log(x[userid]); // looks for x['userId'] which it will find
Have a look at mdn docs
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55037844%2fwhy-i-cant-access-json-properties-with-dot-operator-while-passing-a-var-thr%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
When you want to access an object's (JSON) property dynamically (using a variable or expression), you should use square bracket notation.
[your expression or variable inside square brackets]
. i.e, json[useridno]
var usersidno = $("#hiddenid").html();
$.getJSON(
"http://localhost/example/file.json",
function(json)
console.log(json[usersidno]); // try to use square brackets
);
add a comment |
When you want to access an object's (JSON) property dynamically (using a variable or expression), you should use square bracket notation.
[your expression or variable inside square brackets]
. i.e, json[useridno]
var usersidno = $("#hiddenid").html();
$.getJSON(
"http://localhost/example/file.json",
function(json)
console.log(json[usersidno]); // try to use square brackets
);
add a comment |
When you want to access an object's (JSON) property dynamically (using a variable or expression), you should use square bracket notation.
[your expression or variable inside square brackets]
. i.e, json[useridno]
var usersidno = $("#hiddenid").html();
$.getJSON(
"http://localhost/example/file.json",
function(json)
console.log(json[usersidno]); // try to use square brackets
);
When you want to access an object's (JSON) property dynamically (using a variable or expression), you should use square bracket notation.
[your expression or variable inside square brackets]
. i.e, json[useridno]
var usersidno = $("#hiddenid").html();
$.getJSON(
"http://localhost/example/file.json",
function(json)
console.log(json[usersidno]); // try to use square brackets
);
answered Mar 7 at 7:17
Bear NithiBear Nithi
2,596723
2,596723
add a comment |
add a comment |
This is a working example of ,your issue I have used an api ,that gives back a json ,You can use [] annotatiton to access object properties.
$(document).ready(function()
var user = $("#hiddenid").html()
console.log(typeof user)
$.getJSON(
"https://jsonplaceholder.typicode.com/todos/1",
function(json)
let js=JSON.parse(JSON.stringify(json))
console.log(js[user]);
console.log(js.user);//undefined
console.log(js)
);
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="hiddenid">userId</div>
Explanation:
var userid = 'userId';
console.log(x.userid); // it looks for x.userid, which throws undefined,if it does'nt exists
console.log(x[userid]); // looks for x['userId'] which it will find
Have a look at mdn docs
add a comment |
This is a working example of ,your issue I have used an api ,that gives back a json ,You can use [] annotatiton to access object properties.
$(document).ready(function()
var user = $("#hiddenid").html()
console.log(typeof user)
$.getJSON(
"https://jsonplaceholder.typicode.com/todos/1",
function(json)
let js=JSON.parse(JSON.stringify(json))
console.log(js[user]);
console.log(js.user);//undefined
console.log(js)
);
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="hiddenid">userId</div>
Explanation:
var userid = 'userId';
console.log(x.userid); // it looks for x.userid, which throws undefined,if it does'nt exists
console.log(x[userid]); // looks for x['userId'] which it will find
Have a look at mdn docs
add a comment |
This is a working example of ,your issue I have used an api ,that gives back a json ,You can use [] annotatiton to access object properties.
$(document).ready(function()
var user = $("#hiddenid").html()
console.log(typeof user)
$.getJSON(
"https://jsonplaceholder.typicode.com/todos/1",
function(json)
let js=JSON.parse(JSON.stringify(json))
console.log(js[user]);
console.log(js.user);//undefined
console.log(js)
);
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="hiddenid">userId</div>
Explanation:
var userid = 'userId';
console.log(x.userid); // it looks for x.userid, which throws undefined,if it does'nt exists
console.log(x[userid]); // looks for x['userId'] which it will find
Have a look at mdn docs
This is a working example of ,your issue I have used an api ,that gives back a json ,You can use [] annotatiton to access object properties.
$(document).ready(function()
var user = $("#hiddenid").html()
console.log(typeof user)
$.getJSON(
"https://jsonplaceholder.typicode.com/todos/1",
function(json)
let js=JSON.parse(JSON.stringify(json))
console.log(js[user]);
console.log(js.user);//undefined
console.log(js)
);
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="hiddenid">userId</div>
Explanation:
var userid = 'userId';
console.log(x.userid); // it looks for x.userid, which throws undefined,if it does'nt exists
console.log(x[userid]); // looks for x['userId'] which it will find
Have a look at mdn docs
$(document).ready(function()
var user = $("#hiddenid").html()
console.log(typeof user)
$.getJSON(
"https://jsonplaceholder.typicode.com/todos/1",
function(json)
let js=JSON.parse(JSON.stringify(json))
console.log(js[user]);
console.log(js.user);//undefined
console.log(js)
);
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="hiddenid">userId</div>
$(document).ready(function()
var user = $("#hiddenid").html()
console.log(typeof user)
$.getJSON(
"https://jsonplaceholder.typicode.com/todos/1",
function(json)
let js=JSON.parse(JSON.stringify(json))
console.log(js[user]);
console.log(js.user);//undefined
console.log(js)
);
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="hiddenid">userId</div>
edited Mar 7 at 7:50
Cody Gray♦
194k35381469
194k35381469
answered Mar 7 at 7:24
Shubh DixitShubh Dixit
1
1
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55037844%2fwhy-i-cant-access-json-properties-with-dot-operator-while-passing-a-var-thr%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
in which variable you get
undefined
?– Bear Nithi
Mar 7 at 7:02
can you try logging 'json' object that you are receiving inside the function and see if there exists a property inside json with name useridno? as it seems there's no property named useridno inside the object.
– Zeeshan Adil
Mar 7 at 7:03
Shouldn't Harry, James, and Ben have comas between them?
– Chipster
Mar 7 at 7:11
if i console.log(json) i get all the name, as normal.
– Mic
Mar 7 at 7:14
2
Looks like what you're trying to do here is
json[usersidno]
, notjson.usersidno
. You want to get the property whose name matches the value inusersidno
, not the property namedusersidno
.– JLRishe
Mar 7 at 7:15