How to parse .json file using jquery
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 th开发者_如何转开发is.
Thanks,
- 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);
});
精彩评论