0

I am looking for a way to parse a local JSON file from a none-hosted HTML page (e.g.: file:///C:/Folder/To/Your/HTML/default.html) without using AJAX since it will only entail an allow access-origin error. I also cannot use JSONP since the HTML is not hosted to any hosting server (whether locally or remotely hosted).

3
  • can you use a file input? Commented Nov 19, 2015 at 6:18
  • 3
    Possible duplicate of Loading local json file Commented Nov 19, 2015 at 6:25
  • Maybe you can load it in an iframe using the src attribute and parse the contents using innerHTML. Commented Nov 19, 2015 at 6:35

1 Answer 1

0

suppose your json file 'json.data' is like below:

data = '[{"name" : "Harry", "age" : "32"}]';

you have to load the json file into html head section like below:

     <script type="text/javascript" src="file.json"></script>

and then in body section you have to call the javascript function to parse json data like..

<body onload="load();"></body>

javascript code:

<script>
function load() {
    var mydata = JSON.parse(data);
    alert(mydata[0].name);
    alert(mydata[0].age);
}
</script>

that's it.

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

2 Comments

you can also see this thread - stackoverflow.com/questions/17944344/…
you don't parse script files, and the code show simply won't work.

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.