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).
-
can you use a file input?dandavis– dandavis2015-11-19 06:18:03 +00:00Commented Nov 19, 2015 at 6:18
-
3Possible duplicate of Loading local json fileIanB– IanB2015-11-19 06:25:49 +00:00Commented Nov 19, 2015 at 6:25
-
Maybe you can load it in an iframe using the src attribute and parse the contents using innerHTML.DDS– DDS2015-11-19 06:35:50 +00:00Commented Nov 19, 2015 at 6:35
Add a comment
|
1 Answer
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.
2 Comments
user-4653387
you can also see this thread - stackoverflow.com/questions/17944344/…
dandavis
you don't parse script files, and the code show simply won't work.