Justin
Justin

Reputation: 1299

Get php to output Json with out quotes so that javascript will see it as a object

Here is the problem I am having I would like to be able to output a javascript object like this

{
    id : "title",
    name : "Title",
    field : "title",
    width : 200,
    cssClass : "cell-title",
    editor : Slick.Editors.Text
}

Notice that the editor : Slick.Editors.Text is not in quotes of any kind.

I can output this... but I can't seem to get php to not put quotes around Slick.Editor.Text

{"id":"title","name":"Title","field":"title","width":200,"cssClass":"cell-title","editor":"Slick.Editors.Text"}

Here is the php code i am using to output this string.

public function creatColumn($id, $name, $field, $width, $cssClass, $editor = null) {
    $obj = (object) array('id'=>$id, 'name'=>$name, 'field'=>$field, 'width'=>$width, 'cssClass'=> $cssClass, 'editor' => $editor);
    return json_encode($obj);
}

Is there a way to output a php json object to php doesn't quote the string?

Upvotes: 0

Views: 104

Answers (1)

dave
dave

Reputation: 64657

It's a little hacky, but you could do:

public function creatColumn($id, $name, $field, $width, $cssClass, $editor = null) {
  $obj = (object) array('id'=>$id, 'name'=>$name, 'field'=>$field, 'width'=>$width, 'cssClass'=> $cssClass);
  $json = json_encode($obj);
  return str_replace('}', '"editor":'.$editor.' }', $json);
}

Upvotes: 2

Related Questions