Reputation: 10078
I have the following ajax call and the json feed it returns. how do I get the value of the data object i.e. FRI from the feed using jquery?
$.ajax({
url: query,
type: "GET",
dataType: "json"
success: function(data) {
var day = // get data value from json
$("#Day").val(day);
}
});
{
"name":"workdays",
"columns":[
"day"
],
"data":[
[
"FRI"
]
]
}
* update *
What would be the syntax be if the results were returned as jsonp as follows, how can you extract the value 'FRI' :
import({
"Results":{
"work_days":{
"empid":100010918994,
"day":"FRI"
}
}
});
Upvotes: 4
Views: 19411
Reputation: 8423
This is just JavaScript, not jQuery.
var data = {
"name":"workdays",
"columns":[
"day"
],
"data":[
[
"FRI"
]
]
}
data.data[0][0]; //FRI
UPDATE
var obj = {
"Results":{
"work_days":{
"empid":100010918994,
"day":"FRI"
}
}
}
obj.Results.work_days.day //FRI
Upvotes: 4
Reputation: 757
$.ajax({
url: query,
type: "GET",
dataType: "json"
success: function(data) {
var day = data.data[0][0] // get data value from json
$("#Day").val(day);
}
});
{
"name":"workdays",
"columns":[
"day"
],
"data":[
[
"FRI"
]
]
}
Upvotes: 1
Reputation: 17
Have you ever checked the data.d variable?
success: function(data) {
console.log(data.d); //prints the value
}
Upvotes: 0
Reputation: 776
For retrieving the 'data' field
$.ajax({
url: query,
type: "GET",
dataType: "json"
success: function(data) {
var day = data['data']; // get data value from json
$("#Day").val(day);
}
});
{
"name":"workdays",
"columns":[
"day"
],
"data":[
[
"FRI"
]
]
}
Upvotes: 0
Reputation: 170
If the latter json is the json you get from the server, you can get the data like this:
var day = data.data[0][0];
This will put the value FRI
in the variable day
.
EDIT: If you use a recent browser, you can always do console.log(data)
and look in your javascript console what is in the variable
Upvotes: 3
Reputation: 854
You don't need jquery data is a javascript object (hash). You can access the data as follow:
data.columns
Or
data["columns"]
Upvotes: 0
Reputation: 348
json is javascript object, so you can access to any his property:
data.name
data.columns
data.data
Upvotes: 0