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

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

Related

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
}
}

Displaying value transferred from URL via javascript [duplicate]

This question already has answers here:
Get the values from the "GET" parameters (JavaScript) [duplicate]
(63 answers)
Closed 3 years ago.
I'm having an issue with my codes concerning the transfer of information from one page to another using the url as shown below:
window.location.href ="form.html?uname="+uname;
The value is displaying in the url box but when I try to display it on the form.html page using the following code:
window.onload = function ()
{
var name = document.getElementById("uname");
alert(name);
}
The alert keep displaying null.
What is the issue because after an hour of troubleshooting, I can't seem to figure it out.
Is the null being displayed in the alert box means that the value is not being retrieve from the url?
Thanks in advance.
document.getElementById('uname')
looks for an HTML element with the corresponding id. What you probably want to do is:
alert(uname)

Ajax get function array element [duplicate]

This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 7 years ago.
Hello stackoverflow community, I need help with ajax $.get function. When I recieve array from getlist.php and i alert ir like this alert(data_list); everything works correctly. But when I try to alert like this alert(data_list.id) it doesn't work. Here is how my array looks like in console:
[{"id":"2","name":"Something","type":"horizontal","clicks":"0","start_date":"01/20/2016","end_date"
:"02/19/2016","status":"1","target":"http://","image_url":"http://","pre_exp_email":"0"},{"id"
:"1","name":"None","type":"horizontal","clicks":"2","start_date":"01/20/2016","end_date":"05/19/2016"
,"status":"1","target":"http://wps.us.lt","image_url":"http://....../wp-content/uploads/2016
/01/250by250ad.jpg","pre_exp_email":"0","group_id":["1"],"slots":{"1":"1"}}]
And here is myfunction which calls get function.
function get_list() {
jQuery.get("/wp-content/plugins/wp125/functions/getlist.php", { grouptype:jQuery('#grouptype').val() }, function(data_list){
var str;
alert(data_list.name);
});
}
If data is only a object like
var k={"id":123;"name":"abc"}
you can access id using k.id
but when it is object of Array like
var k=[{"id":123,"name":"abc"},{"id":13,"name":"ab"}];
in that case you have iterate over array something like this
for(i=0;i<k.length;i++)
{console.log(k[i].id)}

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

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.

.find('.foo').text() as array [duplicate]

This question already has answers here:
Get array of text values using selector instead of iterating?
(7 answers)
Closed 9 years ago.
I have html with multiple things classed .text. I the perform
console.log($(whatever).find('.foo').text());
The result is a textstring with all the contents in it. Can I save it to an array instead somehow?
You can use the map method to get this done
var textArray = $(whatever).find('.foo').map(function() {
return $(this).text();
}).get();
This will get you an array.. If you want to display it as a string then just use join along with it.
Check Fiddle
var arr = [];
$(whatever).find('.foo').each(function() {
arr.push($(this).text());
});

Categories

Resources