我有JSON需要从中获取一些信息。
<?
$json = {"ship":{"shipname":"Harbinger Navy Issue","shipid":"33155","dna":"33155:3520;6:11561;1:19280;1:19278;1:4349;1:19191;1:2364;2:1248;3:2048;1:31372;3:2488;10:12814;6::"},"high":[{"Heavy Pulse Laser II:3520":6}],"medium":[{"Shield Boost Amplifier I:11561":1},{"Pith A-Type EM Ward Field:19280":1},{"Pith A-Type Thermic Dissipation Field:19278":1},{"Pithum C-Type Adaptive Invulnerability Field:4349":1},{"Pithum A-Type Medium Shield Booster:19191":1}],"low":[{"Heat Sink II:2364":2},{"Capacitor Flux Coil II:1248":3},{"Damage Control II:2048":1}],"rig":[{"Medium Capacitor Control Circuit I:31372":3}],"subsystem":[],"drones":[{"Warrior II:2488":10}],"charge":[{"Conflagration M:12814":6}]};
$obj_o = json_decode($json);
$test = $obj_o->ship->shipname;
$test2 = $obj_o->high->{0};
$test3 = $obj_o->ship->high->{0}->{0};
$test4 = $obj_O->ship->dna;
$test5 = $obj_a[high][1];
//$test6 = $obj_a[medium][1]{1};
$test6 = $result->ship->shipname;
echo 'test6:';
echo $test6;
echo '</br>';
echo 'test5:';
echo $test5;
echo '</br>';
echo $test;
echo '</br>';
echo 'test2:';
echo $test2;
echo '</br>';
echo 'test3:';
echo $test3;
echo '</br>';
?>
我想从JSON中选择“high”并将其拆分为3个部分:
我该怎么做?
答案 0 :(得分:1)
编辑:如果你想将它作为一个数组返回:
$obj_o = json_decode($json, true);
foreach($obj_o["high"] as $key => $value){
$keyArray = explode(":", $key);
//$keyArray[0] is 'High Pulse Laser II'
//$keyArray[1] is 3520
//$value is 6
}
如果你想要一个物体:
$obj_o = json_decode($json);
foreach($obj_o->high as $weapon) {
foreach($weapon as $key => $value) {
$keyArray = explode(":", $key);
//$keyArray[0] is 'High Pulse Laser II'
//$keyArray[1] is 3520
//$value is 6
}
}