如何将对象转换为特定的JSON格式?

时间:2022-01-11 00:17:51

json_encode converts an object to:

json_encode将对象转换为:

{
  "height":10,
  "width":20,
  "depth":5
}

But I need it to include the objects class name as well:

但我需要它包括对象类名称:

{
  "cuboid":
  {
    "height":10,
    "width":20,
    "depth":5
  }
}

4 个解决方案

#1


2  

public function toJson() {
    return json_encode([get_class($this) => $this]);
}

#2


0  

Hope this will help you out. Here we are converting json to array and putting it into an array of field cuboid

希望这会帮助你。这里我们将json转换为数组并将其放入一个字段cuboid数组中

Try this code snippet here

在此处尝试此代码段

<?php

ini_set('display_errors', 1);

$json='{
  "height":10,
  "width":20,
  "depth":5
}';
$result["cuboid"]=json_decode($json,true);
echo json_encode($result,JSON_PRETTY_PRINT);

#3


0  

You can use get_class to get class name of an object.

您可以使用get_class来获取对象的类名。

$json = json_encode(array(get_class($object) => $object));

#4


0  

Rules for json_encode

json_encode的规则

  • if you have an object it will be encode to {'pro1':'value'}
  • 如果你有一个对象,它将被编码为{'pro1':'value'}

  • if you have an array it will be encode to ['value']
  • 如果你有一个数组,它将编码为['value']

  • if you have an string it will be encode to 'value'
  • 如果你有一个字符串,它将被编码为'value'

Note: If you have an assoc-array in php, it becomes an object in json! If you have an index-array it will be an array in json. Dont mix index & assoc!

注意:如果你在php中有一个assoc-array,它就成了json中的一个对象!如果你有一个索引数组,它将是json中的一个数组。不要混合索引和assoc!

Test this bad pratice: echo json_encode(array('foo' => 'bar',1,2)); Result is this bad syntax {"kitten":"test","0":1,"1":2} (propertie-names should NOT be numbers !!!)

测试这个糟糕的实践:echo json_encode(array('foo'=>'bar',1,2));结果是这个错误的语法{“小猫”:“测试”,“0”:1,“1”:2}(属性名称不应该是数字!!!)

So if you want and object under a property name do this

因此,如果您想要和属性名称下的对象执行此操作

$obj = new stdClass();
$obj->prop = array(1,2,'a');
$newObject = new stdClass();
$newObject->objname = $obj;

print_r(json_encode($newObject));

Becomes: {'objname':{'prop':[1,2,'a']}}

Have a nice day :-)

祝你今天愉快 :-)

#1


2  

public function toJson() {
    return json_encode([get_class($this) => $this]);
}

#2


0  

Hope this will help you out. Here we are converting json to array and putting it into an array of field cuboid

希望这会帮助你。这里我们将json转换为数组并将其放入一个字段cuboid数组中

Try this code snippet here

在此处尝试此代码段

<?php

ini_set('display_errors', 1);

$json='{
  "height":10,
  "width":20,
  "depth":5
}';
$result["cuboid"]=json_decode($json,true);
echo json_encode($result,JSON_PRETTY_PRINT);

#3


0  

You can use get_class to get class name of an object.

您可以使用get_class来获取对象的类名。

$json = json_encode(array(get_class($object) => $object));

#4


0  

Rules for json_encode

json_encode的规则

  • if you have an object it will be encode to {'pro1':'value'}
  • 如果你有一个对象,它将被编码为{'pro1':'value'}

  • if you have an array it will be encode to ['value']
  • 如果你有一个数组,它将编码为['value']

  • if you have an string it will be encode to 'value'
  • 如果你有一个字符串,它将被编码为'value'

Note: If you have an assoc-array in php, it becomes an object in json! If you have an index-array it will be an array in json. Dont mix index & assoc!

注意:如果你在php中有一个assoc-array,它就成了json中的一个对象!如果你有一个索引数组,它将是json中的一个数组。不要混合索引和assoc!

Test this bad pratice: echo json_encode(array('foo' => 'bar',1,2)); Result is this bad syntax {"kitten":"test","0":1,"1":2} (propertie-names should NOT be numbers !!!)

测试这个糟糕的实践:echo json_encode(array('foo'=>'bar',1,2));结果是这个错误的语法{“小猫”:“测试”,“0”:1,“1”:2}(属性名称不应该是数字!!!)

So if you want and object under a property name do this

因此,如果您想要和属性名称下的对象执行此操作

$obj = new stdClass();
$obj->prop = array(1,2,'a');
$newObject = new stdClass();
$newObject->objname = $obj;

print_r(json_encode($newObject));

Becomes: {'objname':{'prop':[1,2,'a']}}

Have a nice day :-)

祝你今天愉快 :-)