简体   繁体   English

如何使用jQuery解析.json文件

[英]How to parse .json file using jquery

I want to read .json file using jquery. 我想使用jquery读取.json文件。

  • It should read the .json file Eg : abc.json 它应该读取.json文件,例如:abc.json

     { "data": [ { name: "Brad", rollno: "1" }, { name: "John", rollno: "2" } ] } 
  • After reading it should return result in normal javascript array. 读取后,应以普通javascript数组返回结果。

Please let me know your pointer in this. 请让我知道您的指针。

Thanks, 谢谢,

  1. Fix the data so it conforms to the JSON specification 修复数据 ,使其符合JSON规范
  2. Use getJSON 使用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. 但是,data属性的值是一个Array。

$.parseJSON从json对象返回一个数组。

First, technically that is not JSON, as all of your keys are not quoted. 首先,从技术上讲,这不是JSON,因为您的所有键均未引用。 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: 如果要将数据数组中的所有对象格式化为key = value,则可以执行以下操作:

var myArray = [];
$.each(yourJSONVar.data, function(index, object) {
    myArray.push(object.name + "=" + object.rollno);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM