1

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,

2
  • 2
    That isn't JSON. jsonlint.com Commented Apr 5, 2011 at 6:51
  • How do you read the file? Does it come directly from a request to a webserver? Commented Apr 5, 2011 at 6:52

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

$.parseJSON returns an array from a json object.

Comments

1

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);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.