如何在PHP中以'复杂'结构使用json?
我想用我的数据使用json + php。我读了更多的文档来做到这一点,基本功能是json_decode()和json_encode()。我的问题是,阅读更多文档和阅读结构的不同例子在我身上产生了很多疑问。如何在PHP中以'复杂'结构使用json?
我想创建这样begine从基本到容器的结构:
- 有一个基地,即有2个属性:ID和值
- 有一个可以有多个基地一个操作
- 有一个命令,可以有多个操作(如果可能的属性callad名)
在我心中的结构是这样的...
[ //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结构?并设置一个命令后插入一个新的操作或删除操作?
感谢的所有..
// OK回答时我做了这个代码
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工作,我会从构建本地的PHP类对象(json_encode可与PHP对象以及):
class Base {
var $id;
var $value;
}
然后,只需要将这些对象放入各种数组中,您也可以使用诸如addToOperation($baseObj)
和addToCommands($operationObj)
之类的方法进行抽象。
您正在处理本机数据结构(Arrays),因此您可以使用本地方法删除(array_pop)和添加(array_push)数据。
像这样的东西应该工作
// 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
}
}
希望这会有所帮助。
是的,但我想要一个完整的经理例子结构...例如我可以如何添加另一个命令? – user473349 2010-11-12 09:16:49
只是把命令放在其他地方$ data = array('commands'=> array(0 => array)。我认为这是最好的管理,你可以想出 – Belinda 2010-11-12 09:22:09
如果你想给命令一个名字,可以在操作之前添加名称作为命令的属性将您的结构视为多维数组,构建该数组并使用json_encode是复杂数据的最佳技术 – Belinda 2010-11-12 09:32:12
json列表类型[]
等于在PHP中没有键的数组。
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;只有一个问题是否有一种方法来添加一个属性到数组(...)在我的情况下的操作? – user473349 2010-11-12 09:40:59
与您的代码我试试这个类的基地 \t { \t变量$ ID; \t变量$值; \t \t function __construct($ i,$ v) \t { \t \t $ this-> id = $ i; \t \t $ this-> value = $ v; \t} \t} \t \t $ a = new Base('1','11'); \t $ b = new Base('2','10'); \t \t \t $ commans1 = Array($ a,$ b); \t $ commans2 = Array($ a,$ b); \t \t $ tot = Array($ commans1,$ commans2); \t \t echo json_encode($ tot); 如何添加属性名称到操作? – user473349 2010-11-12 09:57:35