Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get value from json object using jquery?

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"
     }
  }
});
like image 434
adam78 Avatar asked Feb 08 '23 08:02

adam78


2 Answers

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
like image 193
Arg0n Avatar answered Feb 10 '23 21:02

Arg0n


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

like image 34
Ronny Coolen Avatar answered Feb 10 '23 22:02

Ronny Coolen