admin管理员组文章数量:1389762
I am only asking the question because I've spent the last 2 days probably reading through countless other questions that are similar and tutorials and still can't get this to work.
I have a local .json file that I want to load up and parse with JavaScript. The file is called 'fakeData.json'. Its format is as such:
{"UIGroup": {"Parent": null, "Type": "public"}}
I'm using this to try to load the file:
<script src="fakeData.json"></script>
I am using this to try to parse the file:
var jsonData = JSON.parse('fakeData.json');
I am getting these errors:
Uncaught SyntaxError: Unexpected token : fakeData.json:1
Uncaught SyntaxError: Unexpected token ILLEGAL : planetPage.html:11
May someone please help me, thanks.
I am only asking the question because I've spent the last 2 days probably reading through countless other questions that are similar and tutorials and still can't get this to work.
I have a local .json file that I want to load up and parse with JavaScript. The file is called 'fakeData.json'. Its format is as such:
{"UIGroup": {"Parent": null, "Type": "public"}}
I'm using this to try to load the file:
<script src="fakeData.json"></script>
I am using this to try to parse the file:
var jsonData = JSON.parse('fakeData.json');
I am getting these errors:
Uncaught SyntaxError: Unexpected token : fakeData.json:1
Uncaught SyntaxError: Unexpected token ILLEGAL : planetPage.html:11
May someone please help me, thanks.
Share Improve this question edited Nov 21, 2013 at 23:10 Paul D. Waite 99k57 gold badges203 silver badges271 bronze badges asked Nov 21, 2013 at 23:03 Brian LuiBrian Lui 273 silver badges8 bronze badges 4- Unless it's JSONP, referencing the file won't do much good, you need ajax. – adeneo Commented Nov 21, 2013 at 23:04
- How would I change my current JSON to a JSONP? I want to achieve this without the use of jquery, ajax, or anything else; basically as simple as it gets. – Brian Lui Commented Nov 21, 2013 at 23:07
- JSONP is a JavaScript program that consists of a function call (to a function that has to be defined elsewhere in the page) with one argument. – Quentin Commented Nov 21, 2013 at 23:08
-
You can just change it to
var myJSON = {"UIGroup": {"Parent": null, "Type": "public"}}
, include the file before any scripts that use it, and reference it by the variable name. – adeneo Commented Nov 21, 2013 at 23:08
4 Answers
Reset to default 3If you want it as simple as it gets, then I would prefix your json content with
var jsonData = {"UIGroup": {"Parent": null, "Type": "public"}}....
which turns your file into a valid js file and then load it with
<script src="fakeData.json.js"></script>
after that, jsonData will have the required content because of literal object notation.
There is no way that you can load a json file into your page otherwise without ajax/httprequest.
You need to get the JSON text into a string variable before you can parse it.
This is generally achieved using the XMLHttpRequest object.
<script src="fakeData.json"></script>
will attempt to parse the JSON as JavaScript. This will either throw an error (as it does in your case) or create a data structure and then immediately discard it as it isn't assigned there.
var jsonData;
function reqListener () {
jsonData = JSON.parse(this.responseText);
console.log(jsonData);
};
var oReq = new XMLHttpRequest();
oReq.onload = reqListener;
oReq.open("get", "fakeData.json", true);
oReq.send();
If you want to keep it all inline, with a JSON string, and access the object directly you could do something like this:
// copy all the JSON text from the file an keep store it in a local string variable.
var jsonString = '{ "UIGroup": { "Parent": null, "Type": "public" }}';
// parse the string into a JavaScript object
var jsonObj = JSON.parse(jsonString);
// access the object as you usually would
alert(jsonObj.UIGroup.Type);
Fiddle
本文标签: How can I read a json file without jQuery in JavaScriptStack Overflow
版权声明:本文标题:How can I read a .json file without jQuery in JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744698563a2620428.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论