c0dehunter
c0dehunter

Reputation: 6140

Parsing JSON doesn't work

The JS array containing JSON objects is in external file pairs.js which looks like this:

var objects =[{
    "name":"Joe",
    "age":"22"
  },{
    "name":"April",
    "age":"43"
  }, .......
];

I include the file in html like this:

<script src="pairs.js"></script>

Now my problem begins - I can't parse these objects. I've done it like this:

var obj=jQuery.parseJSON(objects);
//also tried: jQuery.parseJSON(objects[0]);
alert(obj.name);

But it won't work. What am I doing wrong?

Upvotes: 0

Views: 2534

Answers (3)

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33534

You are already passing a formed Object, so there is no need to parse it, just try using the object.

Upvotes: 0

Den T
Den T

Reputation: 157

objects is already a JavaScript object and parseJson() won't work in this case

Upvotes: 0

cdhowie
cdhowie

Reputation: 168988

jQuery.parseJSON() expects a string argument that contains JSON. But you're passing it an already-formed object. You don't need to parse anything at all here. Just use objects.

Upvotes: 4

Related Questions