1

I'm making a post request to the server and getting back the array of data that I want, but I can't see to access the individual elements and I can't figure out why.

This is the jist of it

$(document).ready(function() {
  $.post("myscript", { Action: "JQueryReq",  },
  function(data){
   alert(data);
  });
});

If I do the above I get back everything I want and it looks like this (in the JS dialog box)

[{"val1":null,"val2":null,"val3":null,"Size":"Inches","valu4":null}]

But if I change

alert(data);

to

alert(data.Size);

I just get "undefined"

I also tried

var myjsonreturn = eval(data);
alert(myjsonreturn.Size);

I also tried

var myjsonreturn = eval('('+data+')');
alert(myjsonreturn.Size);

And every time I get undefined.

What am I doing wrong?

TIA

2
  • 1
    Please correct your question title to be something a little more informative than "Another" JSON Parsing question. It has something to do with accessing specific elements in JQuery. Please make your title helpful to everyone else with JSON parsing problems. Commented Nov 10, 2009 at 19:29
  • Agreed. This isn't a forum, please ask a real question! Commented Nov 10, 2009 at 19:43

3 Answers 3

1

What is data? Is it a string? If so, you want to use:

eval('('+data+')')[0].Size;
Sign up to request clarification or add additional context in comments.

Comments

0

Tried this?

alert(data[0].Size)

Comments

0

What you're getting back as a JSON response is an array with just one cell. Because the array's length is 1 the index will start from the number 0, so you can access the contents like this:

alert(data[0].Size);

Or, if you want to loop through the values with jQuery's .each():

$.each(data[0], function(index, value){
    alert(index + ':' + value);
});

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.