Load a JSON file and parse it [duplicate] - javascript

This question already has answers here:
Parse JSON in JavaScript? [duplicate]
(16 answers)
Closed 8 years ago.
I need to load a JSON file (done) and then parse it in order to access the data to load the links of a Collapsible Force Layout.
//Load json from local server
$.getJSON("simulator.json", function(json) {
console.log(json);
});

Usually the JSON is gone over in a loop like so:
for(a in json){
//see a child object like this
console.log(json[a]);
//if it has a property, you can acccess it like so:
link = json[a].link;
//if it has child objects you can continue to iterate
for(b in json[a]){
console.log(json[a][b]);
}
}

Related

Put data from a JSON object which was created in php into an array in a different Javascript file [duplicate]

This question already has answers here:
JSON encode MySQL results
(16 answers)
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 7 months ago.
I need to how I can use data that I fetched from the database and put into a JSON Object inside a php-file in another (Javascript)-File, where the data is supposed to end up inside an array.
function loadApplicationList()
{
$email = $_GET['email'];
$listcontent = queryDB("
SELECT Application.FavRank, Proposal.Title, Proposal.pdfName, RegisteredUser.Name
FROM Application
LEFT JOIN user_application
ON Application.ID = user_application.ID AND user_application.eMail = '$email'
LEFT JOIN Proposal
ON Proposal.ID = Application.Proposal_ID
LEFT JOIN RegisteredUser
ON RegisteredUser.eMail = Proposal.AuthorEmail;");
$encodeContent = json_encode($listcontent);
}
?>
Above is the php code, below the JS code
let data = [
// this array is supposed to contain the data from the JSON Object
];
I should also mention that I am new to these languages, so I might've already 'found' the right answer during my hour long research online but wasn't capable to identify it. Thanks for your help in advance.

How to get the Keys in firebase? [duplicate]

This question already has an answer here:
Firebase retrieve child keys but not values
(1 answer)
Closed 4 years ago.
Hey there i am trying to get the random generated key of datasets from a firebase.
like this:
projects
---12345
---67890
...
i just would like to get the key and safe it to another part in the Database.
I tried that:
but it gives me everything under the node.
getKEY(){
firebase.database().ref('project').once('value', function(snapshot) {
console.log("Key" + JSON.stringify(snapshot.val()) )
});
}
can some one help me out with that? How to get only the Key generated by firebase?
i would like to get these Keys and save it to another dataset but i don't know how to access them?
Try the following:
getKEY(){
firebase.database().ref('project').once('value', function(snapshot) {
snapshot.forEach(function(child) {
var keys=child.key;
});
});
}
more info here:
key property

open a csv file and turn values into javascript array [duplicate]

This question already has answers here:
How to parse CSV data?
(14 answers)
Closed 8 years ago.
How can I open a .csv file and turn values into a javascript array.
In classical programming i'd do this by:
opening the file as a string
splitting by ,
close the file
I know how to split, .split(','), but how do I open and close the csv file in javascript or jquery?
$.get(url,function(data)
{
var MainArray = data.split('\n');
for(var i=0;i<MainArray.length;i++)
{
MainArray[i] = MainArray[i].split(',');
// now MainArray is a two dimensional array that contains csv file
//MainArray[row index][column index]
}
}
);

How will I derive Json objects from JS array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have this Json file located in different folder than my js file:
{"Title":"test1", "content":"test2"}, {"Title":"test1", "content":"test2"}
I want to read this file and turn the json into a JavaScript array of objects.
I can use jQuery.
Here you go: http://jsfiddle.net/p3p5P/
Use jQuery.getJSON to load JSON-encoded data from the server using a GET HTTP request. Then use jQuery.parseJSON to takes a well-formed JSON string and return the resulting JavaScript object.
//$.getJSON("yourJsonFile.json", function(myJson) {
//console.log(json); // this will show the info in the console
//hard-code the json for this fiddle example, you will load it with the getJSON statement above
var myJson = '[{"Title":"title1","content":"content1"},{"Title":"title2","content":"content2"}]';
var myJsonObj = $.parseJSON(myJson);
console.log(myJsonObj); //you now have an array of objects.
alert(myJsonObj[0].Title); //how to reference the first title
alert(myJsonObj[1].Title); //how to reference the second title
alert(myJsonObj[0].content); //how to reference the first content
alert(myJsonObj[1].content); //how to reference the second content
//});
NOTE: In this example I turned your json data into an array by enclosing the entire string within [ ]. So you will want to edit your json and change this...
{"Title":"title1","content":"content1"},{"Title":"title2","content":"content2"}
into this...
[{"Title":"title1","content":"content1"},{"Title":"title2","content":"content2"}]
Use jQuery.getJSON to get Json from file,
and then use jQuery.parseJSON to transform into a JS array.
$.getJSON( "ajax/test.json", function( data ) {
var jsarray = jQuery.parseJSON(data);
});

Accessing a two dimensional JSON array with Javascript [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
I have a nested data structure / JSON, how can I access a specific value?
I am using the US Census API and end up with a two dimensional json array from a jQuery.get() request. My result (data) looks like this:
[["P0010001","NAME","state","county","tract"], ["2703","Census Tract 4001.01","17","119","400101"], ["5603","Census Tract 4001.02","17","119","400102"], ["4327","Census Tract 4002","17","119","400200"]]
It looks exactly like a two dimensional javascript array, but I cannot access it like one when I try:
var population = data;
alert(population[1][0]);
Is there a way to convert the json array into a javascript array, or to convert it to a string, which could then be put into an array?
Use JSON.parse:
var population = JSON.parse(data);
alert(population[1][0]);
JsFiddle: http://jsfiddle.net/6CGh8/

Categories