【问题标题】:How use json in php with a 'complex' structure?如何在具有“复杂”结构的 php 中使用 json?
【发布时间】:2011-05-08 23:02:39
【问题描述】:

我想为我的数据使用 json + php。我阅读了更多文档来执行此操作,基本函数是 json_decode() 和 json_encode()。我的问题是,阅读更多文档和阅读不同的结构示例让我产生了很多疑问。

我想创建一个这样的结构,从基础到容器:

  • 有一个 Base,有 2 个属性:id 和 value
  • 有一个可以有多个 Base 的操作
  • 有一个命令可以有多个操作(如果可能,还有一个属性调用名称)

我脑海中的结构是这样的……

[ //The start of Commands

  //Can make a property name here like "name":"puls1"
  [ //Operation1
 { //Base1
   "id":"22398",
   "value":"255"
 },
 { //Base2
   "id":"22657",
   "value":"80",
 },
 { //Base3
   "id":"7928",
   "valore":"15"
 }
  ],

  [ //Operation2
 { //Base1
   "id":"22398",
   "value":"0"
 },
 { //Base2
   "id":"22657",
   "value":"0",
 },
 { //Base3
   "id":"7928",
   "valore":"0"
 }
  ],

] //The close of Commands

但我认为 [ 和 { 的顺序不正确... 我怎样才能制作这样的json结构?并在设置命令后插入新操作或删除操作?

非常感谢..

//通过我制作此代码的回答确定

class Base 
{
  var $i;
  var $value;

  function __construct($i,$v) 
  {
   $this->id = $i;
   $this->value = $v;
   }
}

$a = new Base('1','11');
$b = new Base('2','10');
$c = new Base ('3','20');
$d = new Base ('4','30');

class Operation
{
  var $name;
  var $values = Array();

  function __construct($a) 
  {
   $this->name = $a;
  }

  public function addArray($a)
  {
   array_push($this->values,$a);
  } 
}

$oper1 = new Operation("op1");
$oper1->addArray($a);
$oper1->addArray($b);

$oper2= new Operation("op2");
$oper2->addArray($c);
$oper2->addArray($d);

$commands = Array($oper1,$oper2);

echo json_encode($tot);

现在的问题是如何进行还原操作?这样使用json_decode并封装在其适当的结构中?

【问题讨论】:

    标签: php arrays json multidimensional-array


    【解决方案1】:

    这样的东西应该可以工作

    // Build up your data as a mulitdimensional array
    $data = array(
        'operations' => array(
            0 => array(
               'bases' => array (
                0 => array(
                    'id' => '22398',
                     'value' => 'whatever'
                ),
                1 => array(
                    'id' => 'id goes here',
                     'value' => 'value goes here'
                ),
             1 => array(
                //data for operation 2
             )
    );
    
    // Then use json_encode
    $json = json_encode($data);
    

    我的语法可能并不完美,但这应该会给你这个想法。要访问它,您将使用类似的代码

     $operations = json_decode($data);
     foreach ($operations as $op) {
         foreach ($op->bases as $base) {
             //Logic goes here
          }
     }
    

    希望这会有所帮助。

    【讨论】:

    • 是的,但我想要一个完整的经理结构示例......例如,我如何添加另一个命令?
    • 把命令放在首位 $data = array('commands' => array(0 => array)。我认为这是你能想到的最好的管理方法
    • 如果你想给命令一个名字,你可以在操作之前添加名字作为命令的属性。将您的结构视为多维数组,构建该数组并使用 json_encode 是处理复杂数据的最佳技术。
    【解决方案2】:

    如果您使用 PHP,我会从原生 PHP 类构建对象(json_encode 也适用于 php 对象):

    class Base {
        var $id;
        var $value;
    }
    

    然后只需将这些对象放入各种数组中,您还可以使用addToOperation($baseObj)addToCommands($operationObj) 等方法对其进行抽象。

    您正在处理本机数据结构 (Arrays),因此您可以使用本机方法来删除 (array_pop) 和添加 (array_push) 数据。

    【讨论】:

    • 用你的代码我试试这个类 Base { var $id;变量$值;函数 __construct($i,$v) { $this->id = $i; $this->值 = $v; } } $a = new Base('1','11'); $b = new Base('2','10'); $commans1 = 数组($a,$b); $commans2 = 数组($a,$b); $tot = 数组($commans1,$commans2);回声 json_encode($tot);如何在操作中添加属性名称?
    【解决方案3】:

    json列表类型[]等于php中没有key的数组。

    json字典类型{}等于php中的键控数组。

    你想要的是这样的:

    $json = array(
       array(
         array('id' => $num, 'value' => $val), // Base 1
         array('id' => $num_1, 'value' => $val_1), // Base 3
         array('id' => $num_2, 'value' => $val_2), // Base 2
       ),
       array(...),
       array(...),
    );
    

    【讨论】:

    • 这段代码非常好...如果我想得到一个值,可以使用 $json[0][0]->id;唯一的问题是有没有办法在我的情况下将属性添加到数组(...)到操作中?
    猜你喜欢
    • 1970-01-01
    • 2017-01-26
    • 1970-01-01
    • 2022-08-18
    • 2021-06-25
    • 2011-08-26
    • 1970-01-01
    • 2018-03-31
    • 1970-01-01
    相关资源
    最近更新 更多