could not load json data in d3 js - javascript

i am new in D3 JS. I would like to load json data from D3 js. Here, I am not sure if json is loaded. Here, non of message is appear. I want show html and json data ,
mydata.json
[
{"name":"Hira","age":35},
{"name":"Basu","age":26},
{"name":"Mukhia","age":30}
]
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title of the document</title>
<script src="https://d3js.org/d3.v5.min.js"></script>
</head>
<body>
<script>
d3.json("./mydata.json", function(data) {
console.log(data);
});
</script>
</body>
</html>
Hope if you guys can give some hints.

here, I got a solution.
d3.json("./mydata.json").then(function(data){
console.log(data);
});
In new version we have to specify then function . In then function we have to make call back function.

Related

How to Define Papa Parse Results as Global?

I'm trying to feed a csv file into Chart.js using Papa Parse. I tried to define the result from Papa Parse as a global array, but I found two following examples I'm confused with.
<!doctype html>
<html>
<script src=https://cdnjs.cloudflare.com/ajax/libs/PapaParse/4.6.3/papaparse.min.js>
</script>
<script src=https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js>
</script>
<script>
var helloworld=Papa.parse("1,2,3,4,5").data;
document.write(JSON.stringify(helloworld));
</script>
</html>
This code works well, so I tried to tune it to feed a csv file.
<!doctype html>
<html>
<script src=https://cdnjs.cloudflare.com/ajax/libs/PapaParse/4.6.3/papaparse.min.js>
</script>
<script src=https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js>
</script>
<script>
var helloworld=Papa.parse("csv.csv",{download:true}).data;
document.write(JSON.stringify(helloworld));
</script>
</html>
I thought I did the same thing as the csv.csv file contains just the five numbers separated by commas, but the code printed nothing. Which part do I need to correct? Thank you.

How tho fetch the json data in Javascript for the image attached,it says undefined

image from html codethis is the screenshot of the consolestrong text
2 images attached
When i am trying to fetch the id its saying Undefined. i am trying like response[0].id . if we write like response[0] it shows json object also.
Also if we copy the json data to console and paste the value of response to a variable and then do response[0].id then it works. but from code it doesnot.
Seems a problem with your JSON data.
This is the code i have written working fine with javascript.
<!doctype html>
<html>
<head >
<title>
stack
</title>
<meta charset="utf-8">
</head>
<body>
<button onclick="myFunction()">Click me</button>
</body>
<script>
var response=[{"id":"1", "name":"abhishek","gender":"M"},{"id":"1", "name":"abhishek","gender":"M"}];
function myFunction() {
console.log(response[0].id);
}
</script>
</html>
Hello can you please try something like:
var listdata = [{"id":"1", "name":"abhishek","gender":"M"},{"id":"2", "name":"yogesh","gender":"M"},{"id":"3", "name":"rubi","gender":"F"}];
console.dir(listdata[0].id);
alert(listdata[0].id);
you can see running example here

Can't get specific element of API response [JS]

I can't seem to get a specific part out of the response below, using the code that is also below. I get an "undefined" in the console log when I try to use response["minecraft.net"]. Can someone explain to me why this isn't working?
Response:
"[{"minecraft.net":"green"},{"session.minecraft.net":"green"},{"account.mojang.com":"green"},{"auth.mojang.com":"green"},{"skins.minecraft.net":"green"},{"authserver.mojang.com":"green"},{"sessionserver.mojang.com":"green"},{"api.mojang.com":"green"},{"textures.minecraft.net":"green"},{"mojang.com":"green"}]"
Code:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Mojang Servives Status</title>
</head>
<body>
<script>
var api = new XMLHttpRequest();
api.open("GET", "https://status.mojang.com/check", false);
api.send();
console.log(api);
var response = JSON.parse(api.response)
console.log(response["minecraft.net"]);
</script>
</body>
</html>
The response is wrapped inside of an array. So you'd use response[0]['minecraft.net']

jQuery get entire iframe source including comments outside <html>

The question How do I get the entire page's HTML with jQuery? might look similar, which helps in obtaining data from <html> and <!DOCTYPE>. But in addition, I also require to obtain any comments that persist before the <html> tag.
I am displaying the page with the help of srcdoc attribute using jQuery. My current code to obtain the data is
$("#myiframe").contents().find('html')[0].outerHTML;
and a snippet in this answer to obtain <!DOCTYPE html>
Sample use case:
<!--
This comment will be used for further processing
-->
<!DOCTYPE html>
<html lang='en'>
<head>
...
</head>
<body>
...
</body>
</html>
<!--
This comment will be used for further processing
-->
The current output is only from <!DOCTYPE html> to </html>. Expected to have the comments outside.
Have a look at jQuery's $.get function. This basically returns everything you would retrieve with a normal GET request.
https://api.jquery.com/jquery.get/
Try this:
$('iframe#myiframe').load(function() {
getFrameContent(this.contentWindow.location);
});
function getFrameContent(windowURL) {
$.get( windowURL, function( data ) {
//where 'data' is the page contents
$( "#whateverElementYouWant" ).html( data );
});
}

how to show json data from web service

I have that link http://api.openweathermap.org/data/2.5/weather?q=Andorra+la+Vella that shows on the browser a json with all the values, so I need to pick only the wind, and the temp, but I don't know how to extract it, how to do that?
I thougt in add a callback after "Andorra+la+Vella"&callback=myFunction
And develop a code on javascript to get the values that I need, but I do it and something goes wrong, that's a simple test to try this little application and then i add it to my web:
<!DOCTYPE HTML>
<html lang="es">
<head>
<meta charset="UTF-8">
<title>Test tiempo andorra</title>
<script type="text/javascript">
function showTime(obj) {
var dadIp=document.getElementById('time');
dadIp.innerHTML+=obj.sunset.wind.speed;
}
</script>
</head>
<body>
<div id="time">
<h1>Test of Time in Andorra</h1>
</div>
<script type="text/javascript" src="http://api.openweathermap.org/data/2.5/weather?q=Andorra+la+Vella&callback=showTime"></script>
</body>
</html>
Look at your JavaScript error console.
Uncaught TypeError: Cannot read property 'wind' of undefined
Then look at the JSON source.
obj.sunset doesn't exist. obj.sys.sunset does.
obj.sunset.wind doesn't exist. obj.wind does.
See a live example when the correct object paths are used.
This way for example:
var obj = jQuery.parseJSON( '{ "name": "John" }' );
alert( obj.name === "John" )
;
In case if you need parse DateTime I advise to use moment.js library.

Categories

Resources