Parse local json with ajax, the best way - json

I'm a newbie about Ajax but I'm trying to do this:
I've got a local Json file and I have to do some Ajax calls to read the Json file and insert the values in the HTML file. My Json file is:
{
"item": {
"name": "blabla",
"details": "blablablabla",
"composition": "blablablabla",
"modelDetails": [
"blablablabla",
"blablablabla",
"blablablabla"
],
"images": [
"http://...jpg",
"http://...jpg",
"http://...jpg",
"http://...jpg"
]
}
}
What is the best way to do this? I would like that pushing the button number one will call the first json file, with button two the second json file,...
Thank you
Edit:
The links are very usefull! Thank you!

Here's a nice example of ajax call to retreive JSON data from file and parse JSON data. Hope it helps :)
http://code.runnable.com/UhY_jE3QH-IlAAAP/how-to-parse-a-json-file-using-jquery
IF link doesnt work :) Then try this script
$(document).ready(function() {
//after button is clicked we download the data
$('.button').click(function(){
//start ajax request
$.ajax({
url: "data.json",
//force to handle it as text
dataType: "text",
success: function(data) {
//data downloaded so we call parseJSON function
//and pass downloaded data
var json = $.parseJSON(data);
//now json variable contains data in json format
//Now you can get your data like json.name, json.object
}
});
});
});

Related

How can I load data from JSON Object... not JSON Array

I'm using this standard code from the tutorial examples:
agGrid
.simpleHttpRequest({
url: 'https://www.ag-grid.com/example-assets/olympic-winners.json',
})
.then(function (data) {
gridOptions.api.setRowData(data);
});
But the JSON data I want to use is returned as an Object, not an array.
Like this:
{
"response": [
{"athlete":"Michael Phelps","age":23,"country":"United States","year":2008,"date":"24/08/2008","sport":"Swimming","gold":8,"silver":0,"bronze":0,"total":8},
{"athlete":"Michael Phelps","age":19,"country":"United States","year":2004,"date":"29/08/2004","sport":"Swimming","gold":6,"silver":0,"bronze":2,"total":8}
]
}
Their example files are arrays and start with a [
I can't seem to find any setRowData documentation to see if there's a way around this.
Any ideas?
thx
you'll get the array from data replacing data with data.response :
agGrid
.simpleHttpRequest({
url: 'https://www.ag-grid.com/example-assets/olympic-winners.json',
})
.then(function (data) {
gridOptions.api.setRowData(data.response);
});
Best !

How to load json file?

I am just trying to load JSON file into my react project. I did it like this:
fetch(dane).then(response => {
console.log(response);
return response.json();
}).then(dane => {
console.log(dane);
}).catch(err => {
console.log("Error Reading data " + err);
});
and I imported this file into project:
import data from "./data.json";
but I am getting error like this:Unexpected token < in JSON at position 0
How can I fix it?
If I am asking about something very stupid, just sorry.
The json file is in src folder.
You would only use fetch to get the data from an API and assign the response to a variable that you can call.
Since you are using a mock json response stored in a file, you can do the following.
Example:
A file called data.json containing the following.
{
"name": "John",
"age": "47",
"role": "admin"
}
Can be imported via:
import data from "./data.json"
Then used in your component however you wish.
<h1>Hello {data.name}.</h1>
<h2>Logged in as: {data.role}</h2>
Or if used outside of the render, like in a function:
console.log(data.role)
Also, Unexpected token < in JSON at position 0 seems like your JSON is improperly formatted.
The JSON file should only contain valid JSON data, as shown in my basic example above.

Raw json is not appearing on the page

I am trying to do a 'get' request through ajax on a url. everything works fine on the console but unable to get the raw json data to display on the page. here data.collections gives me an array of objects. I specifically want raw json data only on the page.
my script is this:
var research;
$.ajax({
url: url,
type: 'GET',
dataType: 'json'})
.done(function(data){
research= JSON.stringfy(data.collections)
});
$('.rsh').html(research);
html is this
<div class = 'rsh'></div>
I want raw json data on page like this but my page is blank.
{
"lab": {
"type": "xy",
"year": "yz",
"team": "yx"
},
"name": "qwerty",
"assistants": 5,
}
edit: this question deals with displaying data in raw json format on the page. Unfortunately the problem in my code was wrong placement of the code. Initial question has nothing to do with asynchronous nature although it is good to know.
var research;
$.ajax({
url: url,
type: 'GET',
dataType: 'json'})
.done(function(data){
research= JSON.stringfy(data.collections)
$('.rsh').html(research);
});
An ajax call is asynchronous so you will need to use the response within the done/success method.

How to update json file in AngularJS

I want to update the existing json file in AngularJS
JSON file:
{
"text1": "Click here to edit!",
"text2": "Click here to edit!",
"text3": "Click here to edit!",
"text4": "Click here to edit!"
}
I want to update this JSON file as:
text1: "Abc"
and save this changes in JSON file
You can not update a json file without using a server-side language like PHP or python. Basically it is security compliance. For more understanding kindly go through
https://docs.angularjs.org/guide/security
https://docs.angularjs.org/api/ng/directive/ngCsp
and
https://developer.mozilla.org/en-US/docs/Web/Security/CSP
Imagine you have getjson.php and savejson.php in the server which work exactly as their names suggest.
Now use $http service of Angular to retrieve your json from the server.
$http.get("getjson.php").then(function(response){
$scope.myJsonObject = response.data;
//Your json becomes JS object here. Change it the way you want
$scope.myJsonObject.text1 = "Abc";
});
Use $http service again to send your json back to the server.
$http({
method: "post",
url: "savejson.php",
data: $scope.myJsonObject,
headers: { 'Content-Type': 'application/json; charset=utf-8' }
});
This is the basic. Please note that you need to do your php part to save/load your json file. Also you should handle errors of the $http service.
Please see how $http service and promises work.

highstock with json data

I want use highstock to show my data in the web ,but i have someproblem with the json data,
the Example of basic-line :http://www.highcharts.com/stock/demo/basic-line
http://jsfiddle.net/gh/get/jquery/1.7.2/highslide-software/highcharts.com/tree/master/samples/stock/demo/basic-line/
i change the javascript file the
$.getJSON('http://www.highcharts.com/samples/data/jsonp.php?filename=aapl-c.json&callback=?', function(data)
to the
$.getJSON('data.json', function(data)
i want use the json data in my web folder and i put data in the json file like :
[[1143072000000,60.16],
[1143158400000,59.96],
[1143417600000,59.51],
[1143504000000,58.71],
[1143590400000,62.33],
[1143676800000,62.75],
[1143763200000,62.72],
/* Apr 2006 */
[1144022400000,62.65],
[1144108800000,61.17],
[1144195200000,67.21],
[1144281600000,71.24]]
but i can see nothing in my web, what is wrong with me? is it the json data Format wrong?or others thanks for give me some help
In case when you use JSON, then you should get rid of all comments.
So JSON should looks like:
[[1143072000000,60.16],
[1143158400000,59.96],
[1143417600000,59.51],
[1143504000000,58.71],
[1143590400000,62.33],
[1143676800000,62.75],
[1143763200000,62.72],
[1144022400000,62.65],
[1144108800000,61.17],
[1144195200000,67.21],
[1144281600000,71.24]]
and chart code:
$.getJSON('data.json', function(data) {
window.chart = new Highcharts.StockChart({
chart : {
renderTo : 'container'
},
rangeSelector : {
selected : 1
},
series : [{
name : 'AAPL',
data : data
}]
});
});
Rename your file to data.js . For javascript file and not JSON. It's like that for me :)