如何在Java脚本中转换为数组?

时间:2016-09-04 18:09:19

标签: javascript jquery arrays json ajax

我有以下循环将数据作为json发送到ajax函数:

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

                            }       

                            }

结果是:

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

我该怎么做才能正确输入json?

1 个答案:

答案 0 :(得分:0)

这不是有效的JSON。

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

两个}{出现在彼此旁边是不允许的,因为任何体面的JSON linter应该警告过你:

syntax error