0

Bean.php

class Bean  {
  private $ename = '';
 private $fathername='';



function getEname() {
     return $this->ename;
}

 function getFathername() {
     return $this->fathername;
}

 function setEname($ename) {
     $this->ename = $ename;
}

 function setFathername($fathername) {
     $this->fathername = $fathername;
}

}

test.php

require 'Bean.php';
$finaldata = array();
$k = new Bean();
$k->setEname("Sahil Manchanda");
$k->setFathername("Pawan Kumar Manchadna");
$c = new Bean();
$c->setEname("Rahul Khurana");
$c->setFathername("Vijay Kumar Khurana");
$d = new Bean();
$d->setEname("Gourav Arora");
$d->setFathername("Mangat Rai Arora");
$finaldata[] = $k;
$finaldata[] = $c;
$finaldata[] = $d;
echo json_encode($finaldata);

output:

[{},{},{}]

it is not converting array to json. I had created a Bean Class which holds two parameters and then in other file i created three objects of bean class and then store it in an array. When i tried to encode this in JOSN it gives me three empty json array. please tell me what i did wrong.....

Sahil Manchanda
  • 9,812
  • 4
  • 39
  • 89
  • 1
    You can't use `private` properties. – sjagr Feb 17 '15 at 13:19
  • possible duplicate of [PHP json\_encode class private members](http://stackoverflow.com/questions/7005860/php-json-encode-class-private-members) – sjagr Feb 17 '15 at 13:21

3 Answers3

0

The properties are private. You need to use the bean->getName methods to include them in the json. Or make them public

Joe Fitter
  • 1,309
  • 7
  • 11
0

Use your getters like this to make array and return as you wont be able to use private properties.

$finaldata[0]['name'] = $k->getEname();
$finaldata[0]['fathername']  =  $k->getFathername();
$finaldata[1]['name']  =  $c->getEname();
$finaldata[1]['fathername']  =  $c->getFathername();
$finaldata[2]['name']  =  $d->getEname();
$finaldata[2]['fathername']  =  $d->getFathername();


json_encode($finaldata);
Pratik Joshi
  • 11,485
  • 7
  • 41
  • 73
-2

The header just denotes what the content is encoded in. It is not necessarily possible to deduce the type of the content from the content itself, i.e. you can't necessarily just look at the content and know what to do with it. That's what HTTP headers are for, they tell the recipient what kind of content they're (supposedly) dealing with.

Content-type: application/json; charset=utf-8 designates the content to be in JSON format, encoded in the UTF-8 character encoding. Designating the encoding is somewhat redundant for JSON, since the default (only?) encoding for JSON is UTF-8. So in this case the receiving server apparently is happy knowing that it's dealing with JSON and assumes that the encoding is UTF-8 by default, that's why it works with or without the header.

 class Bean  {
 private $ename = '';
 private $fathername='';



Public function getEname() {
     return $this->ename;
}

 Public function getFathername() {
     return $this->fathername;
}

 Public function setEname($ename) {
     $this->ename = $ename;
}

 Public function setFathername($fathername) {
     $this->fathername = $fathername;
}

}
Ravi Chauhan
  • 1,409
  • 13
  • 26