在 PHP 中将对象转换为 JSON 和将 JSON 转换为对象,(类似于 Java 的 Gson 库)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9858448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting Object to JSON and JSON to Object in PHP, (library like Gson for Java)
提问by farhan ali
I am developing a web application in PHP,
我正在用 PHP 开发一个 Web 应用程序,
I need to transfer many objects from server as JSON string, is there any library existing for PHP to convert object to JSON and JSON String to Objec, like Gson library for Java.
我需要从服务器传输许多对象作为 JSON 字符串,是否有任何现有的 PHP 库可以将对象转换为 JSON 并将 JSON 字符串转换为 Objec,例如 Java 的 Gson 库。
回答by ma?ek
This should do the trick!
这应该可以解决问题!
// convert object => json
$json = json_encode($myObject);
// convert json => object
$obj = json_decode($json);
Here's an example
这是一个例子
$foo = new StdClass();
$foo->hello = "world";
$foo->bar = "baz";
$json = json_encode($foo);
echo $json;
//=> {"hello":"world","bar":"baz"}
print_r(json_decode($json));
// stdClass Object
// (
// [hello] => world
// [bar] => baz
// )
If you want the output as an Array instead of an Object, pass true
to json_decode
如果您希望输出为数组而不是对象,请传递true
给json_decode
print_r(json_decode($json, true));
// Array
// (
// [hello] => world
// [bar] => baz
// )
More about json_encode()
更多关于json_encode()
See also: json_decode()
回答by Oshan Wisumperuma
for more extendability for large scale apps use oop style with encapsulated fields.
为了使大型应用程序具有更多的可扩展性,请使用带有封装字段的 oop 样式。
Simple way :-
简单的方法:-
class Fruit implements JsonSerializable {
private $type = 'Apple', $lastEaten = null;
public function __construct() {
$this->lastEaten = new DateTime();
}
public function jsonSerialize() {
return [
'category' => $this->type,
'EatenTime' => $this->lastEaten->format(DateTime::ISO8601)
];
}
}
echo json_encode(new Fruit()); //which outputs:
回声 json_encode(新水果());//输出:
{"category":"Apple","EatenTime":"2013-01-31T11:17:07-0500"}
Real Gson on PHP :-
PHP 上的真正 Gson :-
回答by Kishor Kundan
json_decode($json, true);
// the second param being true will return associative array. This one is easy.
回答by ivanknow
I made a method to solve this. My approach is:
我做了一个方法来解决这个问题。我的做法是:
1 - Create a abstract class that have a method to convert Objects to Array (including private attr) using Regex. 2 - Convert the returned array to json.
1 - 创建一个抽象类,该类具有使用正则表达式将对象转换为数组(包括私有属性)的方法。2 - 将返回的数组转换为 json。
I use this Abstract class as parent of all my domain classes
我使用这个抽象类作为我所有域类的父类
Class code:
班级代码:
namespace Project\core;
abstract class AbstractEntity {
public function getAvoidedFields() {
return array ();
}
public function toArray() {
$temp = ( array ) $this;
$array = array ();
foreach ( $temp as $k => $v ) {
$k = preg_match ( '/^\x00(?:.*?)\x00(.+)/', $k, $matches ) ? $matches [1] : $k;
if (in_array ( $k, $this->getAvoidedFields () )) {
$array [$k] = "";
} else {
// if it is an object recursive call
if (is_object ( $v ) && $v instanceof AbstractEntity) {
$array [$k] = $v->toArray();
}
// if its an array pass por each item
if (is_array ( $v )) {
foreach ( $v as $key => $value ) {
if (is_object ( $value ) && $value instanceof AbstractEntity) {
$arrayReturn [$key] = $value->toArray();
} else {
$arrayReturn [$key] = $value;
}
}
$array [$k] = $arrayReturn;
}
// if it is not a array and a object return it
if (! is_object ( $v ) && !is_array ( $v )) {
$array [$k] = $v;
}
}
}
return $array;
}
}