Gleydson S. Tavares
Gleydson S. Tavares

Reputation: 600

get JSON object javascript

I have the Student.groovy class that hasMany Note.groovy Student Class extend to User.groovy

I have a JavaScript function that takes as a parameter a list of notes in JSON. The code below works.

$.each(data,function(key, value) {
   alert(value.note);
   alert(value.semester)   
});

But if I do alert(value.student); me is returned [object, object] and when I put alert(value.student.toSource()); It's shown ({class: "project.Student", id: 1}) If I do alert(value.student.name); It's shown undefined

Upvotes: 0

Views: 77

Answers (1)

kockburn
kockburn

Reputation: 17636

You're most likely not sending back the correct JSON format to access the student's name.

As of now with the examples you have given you can only access the class and id of a given student.

value.student.class //<-- project.student
value.student.id //<-- 1

To be able to access the student's name you would have to have an object like so:

{
  class: "project.Student",
  id: 1,
  name: "Example name"
}

If you think you are actually sending everything you need from the server just do a little $(document.body).append(data); or console.log(data); on your getJSON success callback so you can see clearly what you have.

I wouldn't be surpized if value.name works by the way. Depends on your json structure.

Upvotes: 1

Related Questions