I'm trying to assign a JSON array from a URL to a variable. Here's my code:
$.getJSON("tljson.json",function(result){
var items = [];
items.push(result);
});
However, 'alerting' the items only returns
[object Object],[object Object],[object Object],[object Object]
What am I doing wrong?
What you're doing wrong is alerting the result. You have an array of four objects, but alert only shows the default text representation of objects, [object Object]. Convert your data to string yourself before printing. For example, instead of alert(result), you can try alert(JSON.stringify(result)).
Also, alert is ugly, annoying and hard to use; if you can, use console.log() and its friends instead, much easier on the programmer. Check the results in the JavaScript console. (This is under the assumption the alert() was for your own debugging benefit; if it's for users, try doing something in HTML instead.)
It already is a variable, result is the json response that you can access the same way you would if you pushed it to items.
Related
I am passing object of arrays from frontend to backend(react to node) i am using the new FormData and append the "user" obj in the form and then i pass formdata object to my api.
user object in console log view
As shown in the pic this is my obj of arrays object.
This is my react js Code i am passing this user object to backend.
const formData = new FormData()
formData.append("user", user)
axios.post('http://localhost:5000/AddEmployee',
formData
)
Below code is Node js Code
console.log(typeof req.body.user)
It is showing us the string in console log view.
And In debugger mode the user object is showing in this form.
'[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]'
How can i access this object.
And i want to access this object in the below mentioned form.
let primary = req.body.user[1].primaryContact;
"How can i access this object."
Convert the array to a json string so you can see what's in it:
console.log(JSON.stringify(req.body.user))
"And i want to access this object in the below mentioned form."
In order to answer this question, we need to see what the structure of user is. Hopefully my first answer should help with that! If you need more help, post the output to my first answer.
I have saved an array in local storage and trying to use it but when I console.log the localstorage.getitem it returns [object HTMLDivElement] I have tried to use .innerhtml or [...] on it but they didn't work. Btw, since it's an array, I have to use JSON.stringify(movieWatchlistArray):
localStorage.setItem("key",JSON.stringify(movieWatchlistArray))
let str =localStorage.getItem("key")
console.log(JSON.parse(str) )
But when I do this, it will return an empty object inside an array while when I console.log the original array it's fine so I tried to do it without JSON part, it worked but returns a string of [object HTMLDivElement]
localStorage.setItem("key",movieWatchlistArray)
let str =localStorage.getItem("key")
console.log(str)
So how to get the content of [object HTMLDivElement] I have used .innerhtml but it says can't use .innerHTML on undefined .
when you trying to save an element in localstorage you should save the .outerHTML of it, not the element itself. although it's not recommended to save the whole HTML in local storage instead save the data as an object and use it in js to render the HTML.
so in this case it will be :
movieWatchlistArray.unshift(document.querySelector(`.movie--${moviePageNumber-1}`).outerHTML)
I have some react code that renders various panels in the UI.
I'm trying to debug, but when I use console statements to show sportsTopBar.propTypes, for example, it prints [object Object]. When I use JSON.stringify it prints empty.
So how can I debug my structures in the console?
Code snippet provided below. The code can be seen in its entirety in the fiddle.
code snippet
sportsTopBar.propTypes = {
sports-layout: React.PropTypes.string.isRequired,
sportsPanelState: React.PropTypes.object.isRequired,
sports: React.PropTypes.object.isRequired
};
console.log("sportsTopBar.propTypes--->" + sportsTopBar.propTypes);
console.log("sportsTopBar.propTypes--->" + JSON.stringify(sportsTopBar.propTypes));
output
sportsTopBar.propTypes--->[object Object]
sportsTopBar.propTypes--->{}
Problem 1
Syntax error
sports-layout: should be "sports-layout":
Problem 2
You're implicitly casting to string
Your comment on this answer made me realize what all this is about. You want to see what this object looks like in the console, and can't get it to output as expected. So, this line:
console.log("sportsTopBar.propTypes--->" + sportsTopBar.propTypes);
Has the problem of using + to concat the leading string to the value you're wanting to see in the console. This concatentation is causing an implicit cast to string of your object, thus the output of [object Object] where you expected to see the actual object. [object Object] is the toString output of an object.
So to get these logged together in the same output, you need to take advantage of console.log's allowance for multiple parameters:
console.log("sportsTopBar.propTypes--->", sportsTopBar.propTypes);
Notice that what I've done is replace the + with ,. Each will now be logged as given, with no casting done on them.
Problem 3
JSON cannot represent functions
Per JSON.org:
JSON is built on two structures:
A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.
I assume that React.PropTypes.string.isRequired is a function. As such, the serialized output of any object with a property that references that function will not contain a representation of that property/method. Further, your object contains only references to that function, so the resulting serialization (after functions are stripped) is the representation of an empty object: {}.
I'm new to JSON and have not been able to get the result I want. In learning, all I want to do is almost a HELLO WORLD for JSON, with the ultimate goal being to display the data in a table.
I have a JSON Call URL that gives me JSON data, and it's formatted correctly.
I have written a script to see if I can get an alert for my JSON data:
Updated
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
$.getJSON("http://li93-171.members.linode.com:8080/BrokerManager/getActiveBrokerNames/?callback=?", function(data){
console.log(data);
});
</script>
Alert shown says "[object Object],[object Object]" -- I'm obviously doing something wrong. Please help!
First of all, JSON stands for JavaScript Object Notation, which means when your JSON gets received by the client, jQuery will automatically change it from a normal string to a more usable Object. That is why all you see in the alert is [object Object] since alert can only display strings. All non-string values will be casted into strings.
alert(data);
alert(data.toString()); //both are the same
To display an Object for debugging usages, use console.log or
$("<pre>").html(JSON.stringify(data, null, 4)).appendTo("body");
http://jsfiddle.net/DerekL/4XayF/
don't use alert use console.log(data) with objects
if you are using chrome click control + shift + i to view your console , IE hit F12
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