javascript - How to parse .json file using jquery - Stack Overflow

I want to read .json file using jquery.It should read the .json fileEg : abc.json{"data":[{n

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
Add a ment  | 

3 Answers 3

Reset to default 4
  1. Fix the data so it conforms to the JSON specification
  2. 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

相关推荐

  • javascript - How to parse .json file using jquery - Stack Overflow

    I want to read .json file using jquery.It should read the .json fileEg : abc.json{"data":[{n

    1天前
    70

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信