user2606353
user2606353

Reputation: 79

Decode json object

Hello here is my json response from ajax call:

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

How to get for example status which is '1', I tryed $.parseJSON(result), but getting

SyntaxError: JSON.parse: unexpected character

maybe bacause there are null's?

here is more code

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

and php (zend)

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

Upvotes: 0

Views: 87

Answers (2)

Saul Berardo
Saul Berardo

Reputation: 2600

Your json response is nothing more than a object inside an array with a single element. So you can access the attribute you want with:

your_response[0].attribute_name

For example, the following code would extract your 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])

Upvotes: 0

underscore
underscore

Reputation: 6877

You should

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

After That you can access elements like bellow

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

Upvotes: 1

Related Questions