如何使用php在json下解码

时间:2015-01-22 10:20:28

标签: php json

如何使用php

解码下面的json字符串
$json = '{"keys":{"1":"Malayalam","2":"Hindi","3":"English","4":"Tamil"}}';

当我申请时

<?php
     print_r(json_decode($json));
?>

结果

stdClass Object ( [keys] => stdClass Object ( [1] => Malayalam [2] => Hindi [3] => English [4] => Tamil ) ) 

如何独立提取语言名称。

4 个答案:

答案 0 :(得分:1)

$json = '{"keys":{"1":"Malayalam","2":"Hindi","3":"English","4":"Tamil"}}';
$d = json_decode($json);

echo $d->keys->{'1'} . "\n";
echo $d->keys->{'2'} . "\n";

$key = '3';
echo $d->keys->{$key} . "\n";

答案 1 :(得分:1)

试试这个:

<?php
$json = '{"keys":{"1":"Malayalam","2":"Hindi","3":"English","4":"Tamil"}}';
?>
<pre>
<?php
$str = json_decode($json, true);
$keys = $str["keys"];
var_dump($keys);
foreach($keys as $key)
 echo $key . "\n";
?>
</pre>

输出:

array(4) {
  [1]=>
  string(9) "Malayalam"
  [2]=>
  string(5) "Hindi"
  [3]=>
  string(7) "English"
  [4]=>
  string(5) "Tamil"
}
Malayalam
Hindi
English
Tamil

答案 2 :(得分:0)

您可以尝试:

$data = json_decode($json, true);
$languages = $data['keys'];

现在在$languages你将获得json所需的部分。

答案 3 :(得分:-1)

试试这个:

$json = '{"keys":{"1":"Malayalam","2":"Hindi","3":"English","4":"Tamil"}}';
$json_dec = json_decode($json);

foreach ($json_dec->keys as $key){
   echo $key;
}