I want to read .json file using jquery.
It should read the .json file Eg : abc.json
{ "data": [ { name: "Brad", rollno: "1" }, { name: "John", rollno: "2" } ] }
After reading it should return result in normal javascript array.
Please let me know your pointer in this.
Thanks,
I want to read .json file using jquery.
It should read the .json file Eg : abc.json
{ "data": [ { name: "Brad", rollno: "1" }, { name: "John", rollno: "2" } ] }
After reading it should return result in normal javascript array.
Please let me know your pointer in this.
Thanks,
Share Improve this question edited Apr 5, 2011 at 6:50 S L 14.3k18 gold badges79 silver badges119 bronze badges asked Apr 5, 2011 at 6:48 pravinpravin 2,1738 gold badges37 silver badges50 bronze badges 2- 2 That isn't JSON. jsonlint. – Quentin Commented Apr 5, 2011 at 6:51
- How do you read the file? Does it e directly from a request to a webserver? – Nick Weaver Commented Apr 5, 2011 at 6:52
3 Answers
Reset to default 4- Fix the data so it conforms to the JSON specification
- Use getJSON
You won't get an Array though, not at the top level at least. That will be resolved as an Object (since it has named key-value pairs), not an Array. However, the value of the data property will be an Array.
$.parseJSON
returns an array from a json object.
First, technically that is not JSON, as all of your keys are not quoted. Second, it really depends on how you want the data formatted. If you want all of the objects in the data array to be formatted as key=value, you could do something like this:
var myArray = [];
$.each(yourJSONVar.data, function(index, object) {
myArray.push(object.name + "=" + object.rollno);
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744928521a4601591.html
评论列表(0条)