GrantU
GrantU

Reputation: 6555

jquery getting value from getJSON

I;m trying to get the value 'content' from my json data

This is the data:

[{"pk": 2, "model": "template", "fields": {"content": "message", "status": "Draft", "user": 16, "name": "test", "created": "2013-05-19T20:59:04Z"}}]

This is what I have tried:

 $.getJSON('/sms/fetch/' + id, function (data) {
           $('#id_content').val($('#id_content').val() + data.fields.content);

        })

but this does not seem to get the value, the value is within the call as shown above.

Upvotes: 0

Views: 52

Answers (2)

ultima_rat0
ultima_rat0

Reputation: 290

It's a array, so: data[0].fields.content

Upvotes: 1

James Allardice
James Allardice

Reputation: 165951

Your data appears to be an array of objects, so you need to access it by index:

console.log(data[0].fields.content); // "message"

Upvotes: 2

Related Questions