I make an AJAX call to fetch some data, get the data back as a JSON Object (not a string).
When I log the object, I get the correct object and it's properties. However, when I try to log one of the objects properties, I get undefined.
For a screenshot of my code:
http://i.imgur.com/gnt3w.gif
For a screenshot of the console log:
http://i.imgur.com/DO09m.gif
What am I doing wrong?
It looks like your POST is returning data in an array, not as an individual object. I bet if you log data[0].bursary_name, you will see the correct logged output.
Related
article is an object variable.
console.log("reesult id " + JSON.stringify(article));
When I do this, it outputs me: [{"Id":43}]
and when I do:
console.log(article[0]);
It outputs me {"Id":43}
but now... HOW to get just 43?
Because when I type:
console.log(article[0].Id);
, It returns me undefined instead of 43. Pf.
So, HOW to get 43?
It is very difficult because I made researches and it does not work as well.
I am unable to comment to so posting as an answer.
Your solution looks fine.
Could you print the output for
JSON.stringify(article[0].id)
Sequelize has it's own way how to return objects in json so you should try using their way to handle with objects since you are using that library.
This is from Sequelize documentation.
For any response object that resulted from a query, you can extract
only the data you want by appending .get({plain:true}) the the
response.
Here you have a link to that documentation so you can read more for that since you are not providing any code to us.
I have an object downloaded from firebase which is of type Bug (Custom type). However, when I tried to console log the output with the following code
console.log('bug',this.bug);
I get the following result. It is what I expected where I can go on and do things like
console.log('company', this.bug.companyName)
However, I want to get that key value as well. Howe can I do that? I tried to do
console.log('company', this.bug.key)
console.log('company', this.bug.$key)
and both of them does not work
try to run
console.log('company', this.bug["$key"])
remember property lookup on objects is either
obj.someKey // or
obj["someKey"]
I have an AJAX call that get's a simple JSON back on success, writing the returned JSON into the variable named data.
The returned JSON has been generated within PHP through:
echo json_encode(array('message'=>0);.
Back in my javascript console I'm logging the output of data with console.log(data);. The output is {"message":0}, which seems correct.
However, if I try to log console.log(data.message); I am getting message: undefined.
What am I doing wrong in accessing this pretty basic JSON object?
I've been reading some articles about how to move through JSON object, e.g. https://www.tutorialspoint.com/json/json_overview.htm but I can't find my mistake.
Hopefully you can point me into the right direction :)
Thank you :)
You have to do JSON.parse(result) first to make it an object, because at the moment you're trying to access message property of a string.
You know there are 2 JSON functions in javascript, right...?
// returns string representation of the object
JSON.stringify(object);
// returns a new object built from the string
JSON.parse(object_stringified);
I'm trying to build a small function where I retrieve the likes of a certain page on Facebook.
My script looks like
$.getJSON('https://api.facebook.com/method/links.getStats?urls=http://www.example.com&format=json',function(data) {
currentdiv.text((data.like_count || 0)+'');
console.log(data.like_count);
});
However, like_count returns undefined.
The data I get back from Facebook is:
[{"url":"http:\/\/www.example.com","normalized_url":"http:\/\/www.example.com\/","share_count":19,"like_count":55,"comment_count":40,"total_count":114,"click_count":0,"comments_fbid":10150397389269673,"commentsbox_count":0}]
The JSON you get back returns an array containing the object.
This should work:
Console.log(data[0].like_count);
JSON response is data of object, so Use this: data[0].like_count
$.getJSON('https://api.facebook.com/method/links.getStats?urls=http://www.naturalpotential.dk&format=json',function(data) {
//currentdiv.text((data.like_count || 0)+'');
console.log(data[0].like_count);
});
DEMO
The data you are getting back is an array containing an object with a like_count property.
You are trying to read the like_count property from the array (where one doesn't exist).
You need to get the object first.
You can object which having index so you have to mention index then you can able to get the like count.
console.log(data[0].like_count);
I searched for this topic, and I can't seem to find the right way to parse the JSON string to read the objects.
Here is my code
$.getJSON("<url>",
function(data) {
alert("success!"+data);
});
outputs:
success![object Object],[object Object],[object Object],[object Object]
Firebug shows correct response, and when I click JSON tab I see all the objects.
Thanks!
When a JSON string is parsed, it is turned into a Javascript object. If you use a string method on an object, the string [object Object] is returned.
You need to use object property access methods instead (e.g. alert(data.somekey);).
Don't use alert() for debugging in cases like this if you have Firebug available. Use console.log(data) and you will get direct insights into your JSON data.
In this case you'd have realized that there's absolutely nothing wrong :D .
JSON = JavaScript Object Notation precisely because it is the way to declare object literals in JavaScript. The data parameter is already a Javascript object (in your case an array of objects) that you can access as:
data[index].fieldname
enter your json string here, and click on the created tree view On the top left you will see how you can access it
link text