php - json_encode result inside json_encode -


i need help, don't on own. may gap me.

i got 2 classes: first 1 (application) has method (tojson) return private variables json-string.

the second (problem) contains first class , able return own content , content of child json.

now, if call tojson-method of superior second class, method calls tojson-method of child.

both tojson-methods using json_encode. logical consequence is, final-result contains escape characters.

{"application":"{\"abbreviation\":\"gb\",\"identifier\":1,\"name\":\"great bounce"\"}"} 

the tojson-method of application similar this:

public function tojson() {     return json_encode(array(         "abbreviation" => $this->_abbreviation,         "identifier" => $this->_identifier->getid(),         "name" => $this->_name     )); } 

the tojson-method of problem:

public function tojson() {     return json_encode(array(         "application" => $this->_application->tojson();     )); } 

the escape chars causing issues javascript. comes solution or different implementation mind?

what inner class returning string, not array representation of itself. outer class encoding string; string contains json data pretty irrelevant. i'd suggest inner class should have method return array representation in addition json representation:

public function tojson() {     return json_encode($this->toarray()); }  public function toarray() {     return array(         "abbreviation" => $this->_abbreviation,         "identifier" => $this->_identifier->getid(),         "name" => $this->_name     ) } 

the outer class takes array representation:

public function tojson() {     return json_encode(array(         "application" => $this->_application->toarray();     )); } 

Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -