-4

I have the following loop to send data to ajax function as json :

foreach($careers->getItems() as $item){


                            if($item->getDepartment()==$departmentID && $item->getLocation()==$locationID) {

                                $arr = array('department' => $item->getDepartment(), 'careerLocation' => $item->getLocation(),
                                'title' => $item->getTitle(), 'job' => $item->getJob(), 'logo' => $item->getLogo());

                            echo json_encode($arr);

                            }       

                            }

The Result is:

 {"department":"2","careerLocation":"1","title":"fd","job":"sa","logo":"sa"}  
{"department":"2","careerLocation":"1","title":"sa","job":"sa","logo":"sa"}

what should I do to type json correctly?

  • That's not valid JSON. It needs to be either one object or an array. – VLAZ Sep 04 '16 at 18:10
  • Possible duplicate of [How can I convert the "arguments" object to an array in JavaScript?](http://stackoverflow.com/questions/960866/how-can-i-convert-the-arguments-object-to-an-array-in-javascript) – Dekel Sep 04 '16 at 18:10

1 Answers1

0

That is not valid JSON.

{  
  "department":"2",
  "careerLocation":"1",
  "title":"fd",
  "job":"sa",
  "logo":"sa"
}{  
  "department":"2",
  "careerLocation":"1",
  "title":"sa",
  "job":"sa",
  "logo":"sa"
}

The two }{ appearing next to each other like that is not allowed, as any decent JSON linter should have warned you:

syntax error

theonlygusti
  • 11,032
  • 11
  • 64
  • 119
  • Still not valid JSON. It's missing the comma. – VLAZ Sep 04 '16 at 18:27
  • @Vld oh wow I didn't notice that the OP had just mashed two Objects together... wtf? – theonlygusti Sep 04 '16 at 18:30
  • ` foreach($careers->getItems() as $item){ if($item->getDepartment()==$departmentID && $item->getLocation()==$locationID) { $arr = array('department' => $item->getDepartment(), 'careerLocation' => $item->getLocation(), 'title' => $item->getTitle(), 'job' => $item->getJob(), 'logo' => $item->getLogo()); echo json_encode($arr); } }` i used this foreach to print json to ajax data, is it correct ? – Mohammed Nazli Sep 04 '16 at 18:37