简体   繁体   中英

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'
    }
  ]
}
$.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.

Please try to see this example.

Jquery to get Json

-or-

How to use a JSON file in javascript

Hope may help you.

Why not to try Google: http://www.json.org/js.html

I used this for Android app that worked with PhoneGap(HTML+Javascript+JQuery)

Worked fine.

My Method:

var link_name = pathtojson.json

$.getJSON(link_name,
function(data){
var ttext = (data["SearchResponse"]["Translation"]["Results"][0]["TranslatedTerm"]);
document.getElementById('ttexti').innerHTML = ttext;
}
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM