14
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'
    }
  ]
}
2
  • 7
    I wouldn't even try to file the local file system. Host the file on a webserver and use XHR. Commented May 20, 2013 at 12:46
  • 4
    thats also not valid JSON, you need double quotes and the property names also need quoting, paste what you have into this web site json.parser.online.fr Commented May 20, 2013 at 12:51

3 Answers 3

14
$.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.

Sign up to request clarification or add additional context in comments.

Comments

0

Please try to see this example.

Jquery to get Json

-or-

How to use a JSON file in javascript

Hope may help you.

Comments

-1

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;
}
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.