Home >Web Front-end >JS Tutorial >How Can I Access External Local JSON Files in JavaScript?
Accessing External Local JSON Files in JavaScript
JSON (JavaScript Object Notation) is a popular format for data exchange, and it can be useful to read JSON data from external local files in JavaScript. Here's how you can do it:
fetch("path/to/json.json") .then(response => response.json()) .then(data => { // Use the JSON data as needed... }) .catch(error => { // Handle any errors here... });
const filePath = "/Users/Documents/workspace/test.json"; fetch(filePath) .then(response => response.json()) .then(data => { // Print the JSON data console.log(data.resource); console.log(data.literals); }) .catch(error => { // Handle any errors here... });
By following these steps, you can easily access external local JSON files in JavaScript and work with the data as needed.
The above is the detailed content of How Can I Access External Local JSON Files in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!