0

您好,这是我来自 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(result),但得到

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);
4

2 回答 2

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]);

演示

在此处输入图像描述

之后,您可以访问如下元素

console.log(a[0].id);
console.log(a[0].period);
于 2013-10-22T17:24:18.953 回答
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])
于 2013-10-22T17:25:01.647 回答