Load Local JSON File
This example demonstrates how to load package a json file with your application and then load it at runtime. The application includes 2 buttons:
1) a load button that will fetch the the data.json file and display a count of the number of objects read in a textarea widget
2) a clear button that will reset the textarea See attachment screen1-60.png
The project source code is attached at the end of this article.
Add JSON file to project
This is the contents of the data.json file that will be packaged with the application:
[
{"key":"123",
"value": "abc"},
{"key":"456",
"value": "def"},
{"key":"789",
"value": "ghi"}
]
See attachment add-json-file.png
Load JSON File
1. Bind OnClick-RunJavaScript action to the function loadJsonData() See attachment runjssnippet.png
Implement loadJsonData() in the load-local-json_custom.js file
function loadJsonData() {
var jqxhr = $.getJSON( "data.json", function(data) {
console.log( "success" );
$('textarea').val("number of objects read: " + data.length);
})
.done(function() {
console.log( "done - success" );
})
.fail(function() {
console.log( "error" );
$('textarea').val("Error loading json file");
})
.always(function() {
console.log( "complete" );
});
}
Project Source Code
See attachment local-json-file.zip
Attachments:
You must be
logged in to view attached files.