解码json对象

时间:2013-10-22 17:14:04

标签: javascript jquery json

你好,这是来自ajax调用的json响应:

[{"id":null,"period":null,"until":null,"agent_id":"15","agent_zlecajacy_id":"15","offer_id":null,"status":"1","tytul":"Pobranie ksi\u0105g","tresc":"Pobranie ksi\u0105g","data_aktualizacji":"2013-10-21","data_kontaktu":"2013-10-08 22:00:00","data_kontaktu_end":"0000-00-00 00:00:00","czas_minuty":"30","created":"2013-10-21","type":"todo","series":null,"offers":"","details":"","parent_id":"0","assignment":null,"color":null,"asigned_users":null,"e_type":null,"show":null}]

如何获得例如'1'的状态, 我尝试了$ .parseJSON(结果),但得到了

SyntaxError: JSON.parse: unexpected character

也许是因为有空的?

这里有更多代码

url: "/schedule/getDetails/?id="+event_id,
        dataType: 'json',
        async: false,
        success : function(json) {
            result = json.result;
                        console.log($.parseJSON(result));

和php(zend)

$result = $model->getDetails($id);
            $this->sendJSON($result);

2 个答案:

答案 0 :(得分:1)

你应该

var a = [{"id":null,"period":null,"until":null,"agent_id":"15","agent_zlecajacy_id":"15","offer_id":null,"status":"1","tytul":"Pobranie ksi\u0105g","tresc":"Pobranie ksi\u0105g","data_aktualizacji":"2013-10-21","data_kontaktu":"2013-10-08 22:00:00","data_kontaktu_end":"0000-00-00 00:00:00","czas_minuty":"30","created":"2013-10-21","type":"todo","series":null,"offers":"","details":"","parent_id":"0","assignment":null,"color":null,"asigned_users":null,"e_type":null,"show":null}];

console.log(a[0]);

DEMO

enter image description here

之后您可以访问bellow

等元素
console.log(a[0].id);
console.log(a[0].period);

答案 1 :(得分:0)

您的json响应只不过是具有单个元素的数组中的对象。因此,您可以访问所需的属性:

your_response[0].attribute_name

例如,以下代码将提取您的agent_id:

myVar= [{"id":null,"period":null,"until":null,"agent_id":"15","agent_zlecajacy_id":"15","offer_id":null,"status":"1","tytul":"Pobranie ksi\u0105g","tresc":"Pobranie ksi\u0105g","data_aktualizacji":"2013-10-21","data_kontaktu":"2013-10-08 22:00:00","data_kontaktu_end":"0000-00-00 00:00:00","czas_minuty":"30","created":"2013-10-21","type":"todo","series":null,"offers":"","details":"","parent_id":"0","assignment":null,"color":null,"asigned_users":null,"e_type":null,"show":null}]

alert(myVar[0].agent_id])