0

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);
user2606353
  • 79
  • 1
  • 8
  • 7
    Your response seems like an javascript object and not a string. `$.parseJSON` takes a string arg. What I am saying is, you don't need to parse.. just read from the object. Try alerting `result[0].status` – Selvakumar Arumugam Oct 22 '13 at 17:15
  • 1
    Are you sure that's the value of `result`? I just tried `JSON.parse()` and it works fine. – Christian Ternus Oct 22 '13 at 17:17
  • http://jsonlint.com appears to validate the JSON you provided. More code would give people more to work with, when trying to figure out where the error is actually occurring. – Jason M. Batchelor Oct 22 '13 at 17:18
  • Assuming it is an actual string, this could be a problem with a BOM character right at the beginning of your json if you have saved your php file as an uft-8 file without turning that off. See [this question](http://stackoverflow.com/questions/17427775/json-encode-creating-a-malformed-json-with-extra-hidden-character) and [this question](http://stackoverflow.com/questions/14532149/bom-in-server-response-screws-up-json-parsing). – Sumurai8 Oct 22 '13 at 17:23

2 Answers2

1

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);
underscore
  • 6,495
  • 6
  • 39
  • 78
0

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])
Saul Berardo
  • 2,610
  • 3
  • 20
  • 24