我有一个JSON对象,我需要得到一些值。我怎么能在JQuery中这样做?

时间:2016-07-08 04:29:48

标签: javascript jquery arrays json

所以我需要获得以下值:

  • 位置
  • 状态
  • abc_status
  • S_S
  • S_E
  • n_c

    {
        "skills": [],
        "languages": [],
        "cal_strs": [{
            "test": null,
            "primary_test": null
        }],
        "id": 123,
        "my_id": 1346,
        "username": "blahblah",
        "full_name": "mr blah",
        "email": "blah@blah.com",
        "location": "boston",
        "manager": "boss",
        "status": 1,
        "abc_status": "here",
        "s_s": "2010-06-08T23:00:00Z",
        "s_e": "2010-06-13T07:00:00Z",
        "n_c": "2010-07-08T07:00:00Z",
        "last_here": null,
    }

我尝试过的事情:

data [0] - 给我开头括号'{' data ['location'] - 返回undefined

3 个答案:

答案 0 :(得分:3)

你在这里有一个对象文字,可以在不使用jquery的情况下操纵,读取这个对象你使用点符号来获得基于键的对象值    obj.key = value

var obj = 
    {
        "skills": [],
        "languages": [],
        "cal_strs": [{
            "test": null,
            "primary_test": null
        }],
        "id": 123,
        "my_id": 1346,
        "username": "blahblah",
        "full_name": "mr blah",
        "email": "blah@blah.com",
        "location": "boston",
        "manager": "boss",
        "status": 1,
        "abc_status": "here",
        "s_s": "2010-06-08T23:00:00Z",
        "s_e": "2010-06-13T07:00:00Z",
        "n_c": "2010-07-08T07:00:00Z",
        "last_here": null
    }
console.log(obj.location);
console.log(obj.status);

var abc_status = obj.abc_status;//save the value to a variable

答案 1 :(得分:1)

您尝试访问json对象,就像它是一个数组一样。不是。你可以访问你的对象,因为它已经是一个合适的javascript对象(它不是json)。只需将该对象存储到变量中(类似于var obj = {...}并输入obj.skills以获取技能数组。如果您想从test数组中获取cal_strs,则可以做obj.cal_strs[0].test

答案 2 :(得分:0)

使用下划线(_。pluck)从对象中获取特定值。 或者尝试定义一个新变量并重新分配它。

var s =  {
        "skills": [],
        "languages": [],
        "cal_strs": [{
            "test": null,
            "primary_test": null
        }],
        "id": 123,
        "my_id": 1346,
        "username": "blahblah",
        "full_name": "mr blah",
        "email": "blah@blah.com",
        "location": "boston",
        "manager": "boss",
        "status": 1,
        "abc_status": "here",
        "s_s": "2010-06-08T23:00:00Z",
        "s_e": "2010-06-13T07:00:00Z",
        "n_c": "2010-07-08T07:00:00Z",
        "last_here": null,
    }
var t = {};
t['location'] = s.location;
t['status'] = s.status;
t['abc_status'] = s.abc_status;
t['s_s'] = s.s_s;
t['s_e'] = s.s_e;
t['n_c'] = s.n_c;

如果多个数组使用下划线。