admin管理员组

文章数量:1244324

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"
     }
  }
});

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"
     }
  }
});
Share Improve this question edited Dec 14, 2015 at 9:38 adam78 asked Dec 7, 2015 at 14:56 adam78adam78 10.1k24 gold badges106 silver badges221 bronze badges 12
  • 1 You access it just as you would a normal Javascript object. I'm not too sure I understand what exactly you are asking... data.data? – Lix Commented Dec 7, 2015 at 14:59
  • day = data['data'] ? – Dragos Commented Dec 7, 2015 at 14:59
  • it's json. (j)ava(s)cript (o)bject (n)otation. you access the decoded json like you would any other javascript data structure, because json IS javascript. – Marc B Commented Dec 7, 2015 at 15:02
  • @MarcB: Saying "json IS javascript" is very misleading. Its syntax looks like a subset of JavaScript, but then some of JavaScript looks like C. So would we say JavaScript IS C? In fact, JSON/JavaScript are even more distinct since JSON is not in any way a programming language. – user1106925 Commented Dec 7, 2015 at 15:07
  • it may be a subset, but far too many people have the idea that json is magically special and requires special handling. – Marc B Commented Dec 7, 2015 at 15:10
 |  Show 7 more ments

7 Answers 7

Reset to default 4

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

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

    $.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"
      ]
   ]
} 

json is javascript object, so you can access to any his property:

data.name
data.columns
data.data

You don't need jquery data is a javascript object (hash). You can access the data as follow:

data.columns

Or

data["columns"]

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"
   ]
]
} 

Have you ever checked the data.d variable?

success: function(data) {
   console.log(data.d); //prints the value
}

本文标签: javascriptHow to get value from json object using jqueryStack Overflow