$getJSON not working - undefined is the result [duplicate] - javascript

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 8 years ago.
Here is the JSON I am trying to parse
{"sites":{"1":{"id":1,"company":"facebook","username":"abc#gmail.com","hint":"mascot"}}}
Here is the javascript parsing it. It was working then the structure of the JSON changed and I cannot for the life of me get it work. The result is 'undefinedundefined' which means that it does not understand what username and hint are. Any thoughts?
$.getJSON('http://localhost:3000/sites.json', 'limit=30', processWebsites);
function processWebsites(data) {
var infoHTML='';
$.each(data, function(website, websiteDetails) {
infoHTML+= websiteDetails.username
infoHTML+= websiteDetails.hint;
});
$('#info').html(infoHTML);
}
and finally the HTML
<body>
<div id = "info">
</div>
</body>

i needed a .sites after data. this fixed it.

I believe felix is correct above. If you just console this, you'll get the value you need.
update: add your breaks
var infoHTML = [];
var data = {"sites":{"1":{"id":1,"company":"facebook","username":"abc#gmail.com","hint":"mascot"}}};
jQuery.each(data.sites, function(){
infoHTML.push(this.username + '<br>' + this.hint);
});
If you happen to get more than one grouping, then just join them with 2 br's if you want some seperation.
$('#info').html(infoHTML.join('<br><br>'));
I am sure there are much more eloquent ways. this is just fast and dirty.

Related

Calling a nested function with a string (window[]) in JS [duplicate]

This question already has answers here:
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Closed 7 months ago.
How to call a nested function with only a string? eg:
function oot(wha) {
function inn(wha)
{
First.innerHTML+="Inner ["+wha+"]";
}
oot.inn = inn;
Second.innerHTML+="Outer ["+wha+"]";
}
oot("1");
oot.inn("2"); //works okay
window["oot"]("3"); //works okay
window["oot.inn"]("4"); //<The problem, doesn't work.
window["oot"]["inn"]("4"); //Works, thanks.
Edited to make the code more readable, and show a solution.
IF there is no way to make this work with a single string i can probably make do, but i will leave the question unanswered for a few hours to see if there is another solution.
You can reference nested Objects like this:
window["oot"]["inn"]("4");
or
window.oot.inn("4")

Get value of json file in javascript(nodejs) [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 9 months ago.
So i want to get a value from a json file, but for the life of it, i cant get to understand what to write.
So if someone could help me write this line of code in javascript node, i would be grateful.
I am trying to get the value of "totalCount"
This is from a library called GeoDb just for context.
main.js(NODEjs)
const timezone = require("./TimezonePortugal.json"); //referencing the json
console.log(timezone.metadata); // i dont know what to say afther this
TimezonePortugal.json
{"data":[{"code":"EUR","countryCodes":["PT","YT","IE","AD","IT","AT","RE","AX","BE","BL","SI","SK","SM","CY","DE","LT","LU","LV","MC","ME","MF","EE","MQ","MT","VA","ES","NL","FI","FR","GF","GP","GR","PM"],"symbol":"€"}],"metadata":{"currentOffset":0,"totalCount":1}}
"." means inside.
If you want to get value of totalCount you have to think like this:
timezone.metadata.totalCount = totalCount inside the metada, metadata inside the timezone
For example:
var timezone=
{
"data":[
{
"code":"EUR",
"countryCodes": ["PT","YT",],
"symbol":"€"
}
],
"metadata": {
"currentOffset":0,
"totalCount":1
}
}

Google script change a value in my array unexpectedly [duplicate]

This question already has answers here:
Why does changing an Array in JavaScript affect copies of the array?
(12 answers)
Closed 5 years ago.
I am stuck with a part of my google script code where one of array elements changed unexpectedly. It looks like a bug to me, but I'd like to hear from here if it's really is or just my coding error that I overlooked. Of course, I'm hoping for a solution as well in any case.
Here is that part of the code.
if (chkIn) {arr[1] = importData[i][1]+'2';
} else {
Logger.log((i)+' '+importData[i][1]);
Logger.log((i+1)+' '+importData[i+1][1]);
Logger.log((i+2)+' '+importData[i+2][1]);
Logger.log(arr[1]);
arr[1] = importData[i][1]+'1';
Logger.log('---------------------------------------------------');
Logger.log((i)+' '+importData[i][1]);
Logger.log((i+1)+' '+importData[i+1][1]);
Logger.log((i+2)+' '+importData[i+2][1]);
Logger.log(arr[1]);
};
(The if statement doesn't seem relevant here, but I included it just in case.)
Here is the output.
2573 2017122103
2574 20171221041
2575 20171221042
20171221042
---------------------------------------------------
2573 2017122103
2574 20171221041
2575 20171221031
20171221031
I really have no idea how importData[i+2][1] changed its value to arr[1] (the number after 2575).
Thank you in advance.
Probably this is because in your case:
arr === importData[i+2]
So when you change arr[1] you also have changed importData[i+2][1].

Display contents of json file in readable alert/console (jQuery) [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 5 years ago.
I am trying to display contents of a json file which has 5 entries as the usual key value pairs, however I currently can only get the Name value to display as intended, the other values are registered literally as a direct return of the in put code. The getJSON method is to start after a button with id of #json is clicked. Its working but only displays the first item with the rest being output literally.
The code is below:
jQuery(document).ready(function($) {
$(document).on('click', '#json', function(event) {
event.preventDefault();
var items;
$.getJSON('somedata.json', function(data) {
items = data;
console.log(items['Name'], ['Phone'], ['Mphone'], ['Email'], ['Message']);
});
});
});
Tips welcome ! Thanks
You're not pulling the other values from items as with the first, do this instead:
console.log(items['Name'], items['Phone'], items['Mphone'], items['Email'], items['Message']);
You forgot to add items before ['Phone'], ['Mphone'] etc..
console.log(items['Name'],items['Phone'], etc..)

parsing json string using json parse method [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 8 years ago.
Hi friends I have a I have a json string as shown below. How to parse the string to get day,min_amount,max_amount values .
[{"day":"1970-01-01","min_amount":"0.00","max_amount":"0.00"},{"day":"1970-01-02","min_amount":"1.00","max_amount":"2.00"}]
Just use JSON.parse. The syntax for accessing a value is simple:
obj = JSON.parse(json)
day = obj[0].day
min_amount = obj[0].day
max_amount = obj[0].day
The great thing about Javascript is how simple it is to use JSON, because JSON is just a serialized version of plain-old javascript hashes, arrays, and scalars.
It's already in object form. SO use this :
var x = [{"day":"1970-01-01","min_amount":"0.00","max_amount":"0.00"},{"day":"1970-01-02","min_amount":"1.00","max_amount":"2.00"}]
jQuery.each(x,function(e){
console.log(x[e])
console.log(x[e].day)
});
Here is the working example : http://jsfiddle.net/u6J8A/
As you may not have noticed, JSON is JavaScript synthax.
<script type="text/javascript">
var data = [
{"day":"1970-01-01","min_amount":"0.00","max_amount":"0.00"},
{"day":"1970-01-02","min_amount":"1.00","max_amount":"2.00"}
];
</script>
Dumping it directly in the JavaScript code is perfectly valid.
But if you are fetching this data at run time and have the information as a string, you can convert it using JSON.parse(string).
The information can be then read from this structure by the variables data[0].day, data[0].min_amount, data[0].max_amount, data[1].day, data[1].min_amount, data[1].max_amount.

Categories

Resources