Skip to content Skip to sidebar Skip to footer

Json_encode With Object Oriented Php

How can I code the following json data using the json_encode in object oriented PHP. var datasets = { 'usa': { label: 'USA', data: [[1988, 483994], [1989, 479060], [1990

Solution 1:

You don't need Object Oriented to do that :

$array = array("usa" => array(
               "label"=>"USA", 
                "data" => array(
                                array("1988","483994"),
                                array("1989","457645") //etc
                          )
                )
          );
 echo json_encode($array);

The same works back with the json string like this :

$string= '{
  "usa": {
    label: "USA",
    data: [[1988, 483994], [1989, 479060], [1990, 457648], [1991, 401949], [1992, 424705], [1993, 402375], [1994, 377867], [1995, 357382], [1996, 337946], [1997, 336185], [1998, 328611], [1999, 329421], [2000, 342172], [2001, 344932], [2002, 387303], [2003, 440813], [2004, 480451], [2005, 504638], [2006, 528692]]
  }

  // skipped other data
}';

print_r(json_decode($string, true)); //Will show you the previous array

See php docs.

Solution 2:

While others has said that you don't need Object Oriented, obviously you want to do it as object oriented as possible. And I think that's a great idea, object orientation is a heck of a lot better than the procedural crap that most PHP programs consists of.

Try this.

<?phpclassDataSet{
    var$label;
    var$data;

    publicfunction__construct($label, $data) {
        $this->label = $label;
        $this->data = $data;
    }
}

classCountryData{
    var$datasets;

    publicfunction__construct() {
        $this->datasets = array();
    }

    publicfunctionadd($label, DataSet $dataset)
    {
        $this->datasets[$label] = $dataset;
    }
}

$usa = new DataSet('USA', array(
    array(1988, 483994),
    array(1989, 479060)
));

$sweden = new DataSet('Sweden', array(
    array(1981, 1000),
    array(1982, 2000)
));

$result = new CountryData();
$result->add('usa', $usa);
$result->add('sweden', $sweden);

echo json_encode($result);

Using this approach you can attach logging and other features to the add method etc, the object oriented approach adds the possibility to use industry standard patterns much more easily.

This is what the code above will output

{
    "datasets":{
        "usa":{
            "label":"USA",
            "data":[[1988,483994],[1989,479060]]
        },
        "sweden":{
            "label":"Sweden",
            "data":[[1981,1000],[1982,2000]]
        }
    }
}

Solution 3:

Arrays can only be numerical in JSON, so associative arrays are encoded the same as objects. You just need to call json_encode as normal:

json_encode($array)

When decoding JSON in PHP if you want an associative array rather than an object, pass true in as the second parameter:

json_decode($json,true);

If you want an object you can pass in false as the second parameter, or not use it as false is the default value for the associative parameter in json_decode.

Post a Comment for "Json_encode With Object Oriented Php"