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);
4
  • 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 Commented Oct 22, 2013 at 17:15
  • 1
    Are you sure that's the value of result? I just tried JSON.parse(<the JSON you posted>) and it works fine. Commented Oct 22, 2013 at 17:17
  • 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. Commented Oct 22, 2013 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 and this question. Commented Oct 22, 2013 at 17:23

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.