I am trying to create a json from a class created in php and I am not getting the expected result, I show you the code I am using and the result I get. Thank you very much in advance and I hope you can help me.
This is the class I want to use:
<?php
namespace App\ClasesEspeciales;
Class ClsItemsPed{
private $ProductCode;
private $SKUCode;
private $VariantCode;
private $Description;
private $VariantDescription;
private $Quantity;
private $DiscountPercentage;
private $UnitPrice;
private $cantKilos;
private $SelectMeasureUnit;
private $MeasureCode;
public function __construct(){
$this->ProductCode = "";
$this->SKUCode = "";
$this->VariantCode = "";
$this->Description = "";
$this->VariantDescription = "";
$this->Quantity = "";
$this->DiscountPercentage = "";
$this->UnitPrice = "";
$this->cantKilos = "";
$this->SelectMeasureUnit = "";
$this->MeasureCode = "";
}
public function getProductCode(){
return $this->ProductCode;
}
public function setProductCode($ProductCode){
$this->ProductCode = $ProductCode;
}
public function getSKUCode(){
return $this->SKUCode;
}
public function setSKUCode($SKUCode){
$this->SKUCode = $SKUCode;
}
public function getVariantCode(){
return $this->VariantCode;
}
public function setVariantCode($VariantCode){
$this->VariantCode = $VariantCode;
}
public function getDescription(){
return $this->Description;
}
public function setDescription($Description){
$this->Description = $Description;
}
public function getVariantDescription(){
return $this->VariantDescription;
}
public function setVariantDescription($VariantDescription){
$this->VariantDescription = $VariantDescription;
}
public function getQuantity(){
return $this->Quantity;
}
public function setQuantity($Quantity){
$this->Quantity = $Quantity;
}
public function getDiscountPercentage(){
return $this->DiscountPercentage;
}
public function setDiscountPercentage($DiscountPercentage){
$this->DiscountPercentage = $DiscountPercentage;
}
public function getUnitPrice(){
return $this->UnitPrice;
}
public function setUnitPrice($UnitPrice){
$this->UnitPrice = $UnitPrice;
}
public function getcantKilos(){
return $this->cantKilos;
}
public function setcantKilos($cantKilos){
$this->cantKilos = $cantKilos;
}
public function getSelectMeasureUnit(){
return $this->SelectMeasureUnit;
}
public function setSelectMeasureUnit($SelectMeasureUnit){
$this->SelectMeasureUnit = $SelectMeasureUnit;
}
public function getMeasureCode(){
return $this->MeasureCode;
}
public function setMeasureCode($MeasureCode){
$this->MeasureCode = $MeasureCode;
}
}
Here I show you the code that I am using, it should be noted that it is a functionality test, I am jarcoding the values in order to be able to do tests, then the filling of the class with the real data will be completed.
$varPedido = new ClsPedido();
$varPedido->setOrderID('1');
$varPedido->setOrderNumber('1');
$varPedido->setDate('1');
$varPedido->setTotal('1');
$varPedido->setTotalDiscount('1');
$varPedido->setPaidTotal('1');
$varPedido->setFinancialSurcharge('1');
$varPedido->setWarehouseCode('1');
$varPedido->setSellerCode('1');
$varPedido->setTransportCode('1');
$varPedido->setSaleConditionCode('1');
$varPedido->setPriceListNumber('1');
$varPedido->setIvaIncluded('1');
$varPedido->setInternalTaxIncluded('1');
$varPedido->setCancelOrder('1');
$varPedido->setCancelReason('1');
$varPedido->setCancelDate('1');
$varPedido->setAgreedWithSeller('1');
$varPedido->setInvoiceCounterfoil('1');
$varPedido->setComment('1');
$varPedido->setOrderCounterfoil('1');
and this is the line I'm using to generate it's json:
$json =json_encode(serialize($varPedido));
Finally, I show you the result that I am getting and as you will see there is my problem:
"O:30:\"App\\ClasesEspeciales\\ClsPedido\":23:{s:39:\"\u0000App\\ClasesEspeciales\\ClsPedido\u0000OrderID\";s:1:\"1\";s:43:\"\u0000App\\ClasesEspeciales\\ClsPedido\u0000OrderNumber\";s:1:\"1\";s:36:\"\u0000App\\ClasesEspeciales\\ClsPedido\u0000Date\";s:1:\"1\";s:37:\"\u0000App\\ClasesEspeciales\\ClsPedido\u0000Total\";s:1:\"1\";s:45:\"\u0000App\\ClasesEspeciales\\ClsPedido\u0000TotalDiscount\";s:1:\"1\";s:41:\"\u0000App\\ClasesEspeciales\\ClsPedido\u0000PaidTotal\";s:1:\"1\";s:50:\"\u0000App\\ClasesEspeciales\\ClsPedido\u0000FinancialSurcharge\";s:1:\"1\";s:45:\"\u0000App\\ClasesEspeciales\\ClsPedido\u0000WarehouseCode\";s:1:\"1\";s:42:\"\u0000App\\ClasesEspeciales\\ClsPedido\u0000SellerCode\";s:1:\"1\";s:45:\"\u0000App\\ClasesEspeciales\\ClsPedido\u0000TransportCode\";s:1:\"1\";s:49:\"\u0000App\\ClasesEspeciales\\ClsPedido\u0000SaleConditionCode\";s:1:\"1\";s:47:\"\u0000App\\ClasesEspeciales\\ClsPedido\u0000PriceListNumber\";s:1:\"1\";s:43:\"\u0000App\\ClasesEspeciales\\ClsPedido\u0000IvaIncluded\";s:1:\"1\";s:51:\"\u0000App\\ClasesEspeciales\\ClsPedido\u0000InternalTaxIncluded\";s:1:\"1\";s:43:\"\u0000App\\ClasesEspeciales\\ClsPedido\u0000CancelOrder\";s:1:\"1\";s:44:\"\u0000App\\ClasesEspeciales\\ClsPedido\u0000CancelReason\";s:1:\"1\";s:42:\"\u0000App\\ClasesEspeciales\\ClsPedido\u0000CancelDate\";s:1:\"1\";s:48:\"\u0000App\\ClasesEspeciales\\ClsPedido\u0000AgreedWithSeller\";s:1:\"1\";s:50:\"\u0000App\\ClasesEspeciales\\ClsPedido\u0000InvoiceCounterfoil\";s:1:\"1\";s:39:\"\u0000App\\ClasesEspeciales\\ClsPedido\u0000Comment\";s:1:\"1\";s:48:\"\u0000App\\ClasesEspeciales\\ClsPedido\u0000OrderCounterfoil\";s:1:\"1\";s:42:\"\u0000App\\ClasesEspeciales\\ClsPedido\u0000objCliente\";N;s:43:\"\u0000App\\ClasesEspeciales\\ClsPedido\u0000objItemsPed\";N;}"
I get the path of the class I'm using + some escaped characters, \u000, anyway, I can't build a clean json.
Thank you very much, greetings and I look forward to your comments.
ISSUE
The problem is the serializer, just as I say in my comment .
If we read the documentation we will see that the method
serialize
:It follows that with this method you will not be able to get the JSON string that represents the attributes of your Class instance (object).
SOLUTION
The solution is simple, you must create your own serializer that you can include in your Class as one more method. Thus, you will be able to call said method as one more of the object. That is, any attribute that does not have an assigned value will be treated as
null
.For example, a method defined in your Class might look like this:
As you can see, it is a very simple serializer that uses two PHP methods:
get_object_vars
, which gets the accessible non-static properties of the given object byobject
according to the scope.Also, the result of
get_object_vars
is an associative Array that can be encoded to JSON without problems byjson_encode
To get the JSON string using this method, you can call it like this:
Using a representation of your Class, the following code:
Produces the following output:
I hope this helps you solve the problem.
Observation
, this is not an answer, I am adding a code example to increase and improve the depth of the analysis that the colleague @Mauricio has exposed, an answer that I fully support as valid.
personally I would implement a class/method that allows me to navigate through recursion and value definition:
Implementation example:
output:
Source: Link
When you use the framework/ORM in your project and define a lot of models, you will find this article useful when JSON serialize these models.
You can regulate the output field of your interface, AJAX interface will often have no more fields or less fields, the same field is a string while it's an integer shame.
Afterword For scalar data in PHP, only floating point data can be overflowed into json_encode . Php7 can be configured via:
serializar_precisión = -1
Also, the browser may not support the long integer type. When the number of order ID digits is relatively long (greater than a 32-bit integer), the JSON browser. Parse() is easy to truncate data. Can be converted to a string during background processing.
I hope what I have added is helpful.