我正在尝试将对象列表输出为json,并且想知道是否有办法使对象可用于json_encode
?我得到的代码看起来像
$related = $user->getRelatedUsers();
echo json_encode($related);
现在,我只是遍历用户数组并单独将它们导出到json_encode
的数组中,以便为我变成可用的json。我已经尝试过使对象可迭代,但json_encode
似乎只是跳过了它们。
编辑:这是var_dump();
php > var_dump($a);
object(RedBean_OODBBean)#14 (2) {
["properties":"RedBean_OODBBean":private]=>
array(11) {
["id"]=>
string(5) "17972"
["pk_UniversalID"]=>
string(5) "18830"
["UniversalIdentity"]=>
string(1) "1"
["UniversalUserName"]=>
string(9) "showforce"
["UniversalPassword"]=>
string(32) ""
["UniversalDomain"]=>
string(1) "0"
["UniversalCrunchBase"]=>
string(1) "0"
["isApproved"]=>
string(1) "0"
["accountHash"]=>
string(32) ""
["CurrentEvent"]=>
string(4) "1204"
["userType"]=>
string(7) "company"
}
["__info":"RedBean_OODBBean":private]=>
array(4) {
["type"]=>
string(4) "user"
["sys"]=>
array(1) {
["idfield"]=>
string(2) "id"
}
["tainted"]=>
bool(false)
["model"]=>
object(Model_User)#16 (1) {
["bean":protected]=>
*RECURSION*
}
}
}
这是json_encode给我的:
php > echo json_encode($a);
{}
我最终得到了这个:
function json_encode_objs($item){
if(!is_array($item) && !is_object($item)){
return json_encode($item);
}else{
$pieces = array();
foreach($item as $k=>$v){
$pieces[] = "\"$k\":".json_encode_objs($v);
}
return '{'.implode(',',$pieces).'}';
}
}
它需要数组充满这些对象或只是单个实例并将它们变成json - 我使用它而不是json_encode。我确信有些地方可以让它变得更好,但我希望json_encode能够检测何时根据其公开的接口迭代对象。
答案 0 :(得分:138)
对象的所有属性都是私有的。又...在他们班级的范围之外不可用。
如果您确实要序列化私有和受保护对象属性,则必须在您的类中实现一个JSON编码函数,该函数在您为此目的创建的数据结构上使用json_encode()
class Thing {
...
public function to_json() {
return json_encode(array(
'something' => $this->something,
'protected_something' => $this->get_protected_something(),
'private_something' => $this->get_private_something()
));
}
...
}
使用新的JsonSerializable
接口提供json_encode
class Thing implements JsonSerializable {
...
public function jsonSerialize() {
return [
'something' => $this->something,
'protected_something' => $this->get_protected_something(),
'private_something' => $this->get_private_something()
];
}
...
}
<强> A more detailed writeup 强>
答案 1 :(得分:38)
在PHP&gt; = 5.4.0中,有一个用于将对象序列化为JSON的新界面:JsonSerializable
只需在对象中实现界面,并定义JsonSerializable
方法,当您使用json_encode
时,该方法将被调用。
所以PHP&gt; = 5.4.0的解决方案应如下所示:
class JsonObject implements JsonSerializable
{
// properties
// function called when encoded with json_encode
public function jsonSerialize()
{
return get_object_vars($this);
}
}
答案 2 :(得分:8)
在RedBeanPHP 2.0中有一个批量导出函数,它将整个bean集合转换为数组。这适用于JSON编码器..
json_encode( R::exportAll( $beans ) );
答案 3 :(得分:3)
以下代码为我工作:
public function jsonSerialize()
{
return get_object_vars($this);
}
答案 4 :(得分:2)
我还没有看到这个,但是bean有一个名为getProperties()
的内置方法。
所以,使用它:
// What bean do we want to get?
$type = 'book';
$id = 13;
// Load the bean
$post = R::load($type,$id);
// Get the properties
$props = $post->getProperties();
// Print the JSON-encoded value
print json_encode($props);
输出:
{
"id": "13",
"title": "Oliver Twist",
"author": "Charles Dickens"
}
现在再往前走一步。如果我们有一系列豆子......
// An array of beans (just an example)
$series = array($post,$post,$post);
...然后我们可以做到以下几点:
使用foreach
循环遍历数组。
用bean的属性数组替换每个元素(bean)。
所以这......
foreach ($series as &$val) {
$val = $val->getProperties();
}
print json_encode($series);
...输出:
[
{
"id": "13",
"title": "Oliver Twist",
"author": "Charles Dickens"
},
{
"id": "13",
"title": "Oliver Twist",
"author": "Charles Dickens"
},
{
"id": "13",
"title": "Oliver Twist",
"author": "Charles Dickens"
}
]
希望这有帮助!
答案 5 :(得分:1)
我通常在我的对象中包含一个小函数,允许我转储到数组或json或xml。类似的东西:
public function exportObj($method = 'a')
{
if($method == 'j')
{
return json_encode(get_object_vars($this));
}
else
{
return get_object_vars($this);
}
}
无论哪种方式,get_object_vars()
对您都有用。
答案 6 :(得分:0)
$products=R::findAll('products');
$string = rtrim(implode(',', $products), ',');
echo $string;
答案 7 :(得分:0)
这是我的方式:
function xml2array($xml_data)
{
$xml_to_array = [];
if(isset($xml_data))
{
if(is_iterable($xml_data))
{
foreach($xml_data as $key => $value)
{
if(is_object($value))
{
if(empty((array)$value))
{
$value = (string)$value;
}
else
{
$value = (array)$value;
}
$value = xml2array($value);
}
$xml_to_array[$key] = $value;
}
}
else
{
$xml_to_array = $xml_data;
}
}
return $xml_to_array;
}
答案 8 :(得分:-1)
对于一个对象数组,我使用了类似的东西,同时遵循php的自定义方法&lt; 5.4:
$jsArray=array();
//transaction is an array of the class transaction
//which implements the method to_json
foreach($transactions as $tran)
{
$jsArray[]=$tran->to_json();
}
echo json_encode($jsArray);