Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read Json file data using javascript?

function uploadJsonFunction(){
  var jsonURL = "C:\Users\My Documents\new\WebContent\JsonFiles\TreeJson\countries.json";
}
  • countries.json is valid json and the above path has countries.json file.
  • I want to read the countries.json file whole data/contents using javascript.

Here is my Json:

{
  identifier: 'id',
  label: 'name',
  items: [
    {
      id: 'AF',
      name: 'Africa',
      type:'continent'
    }
  ]
}
like image 780
Rachel Avatar asked May 20 '13 12:05

Rachel


People also ask

How do I read a JSON file in JavaScript?

We can read this JSON data in JavaScript using the import statement this way: <! ---./index. js--> import data from './data.

How get JSON data from Fetch?

To get JSON from the server using the Fetch API, you can use the response. json() method. The response. json() method reads the data returned by the server and returns a promise that resolves with a JSON object.


1 Answers

$.ajax({
    url: "\countries.json",
    success: function (data) {
        var obj = JSON.parse(data);
    }
});

For safety reasons, The C:\Users\My Documents\new\WebContent\JsonFiles\TreeJson\countries.json url won't work in a browser.

like image 99
Arkadiusz Cieśliński Avatar answered Oct 22 '22 12:10

Arkadiusz Cieśliński