Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP使用JSON获取数据_Php_Json - Fatal编程技术网

PHP使用JSON获取数据

PHP使用JSON获取数据,php,json,Php,Json,我目前正在尝试本地化我的网站,所有数据都将存储在JSON文件中。所以我想知道如何使用php获取数据 这是我的示例JSON文件/数据 { "en-us": { "registration": { "INVALID_USERNAME": "your username is taken", "INVALID_PASSWORD": "your password is not strong", "INVALID_EMAIL": "your email is

我目前正在尝试本地化我的网站,所有数据都将存储在JSON文件中。所以我想知道如何使用php获取数据

这是我的示例JSON文件/数据

{
  "en-us": {
    "registration": {
      "INVALID_USERNAME": "your username is taken",
      "INVALID_PASSWORD": "your password is not strong",
      "INVALID_EMAIL": "your email is invalid"
    },
    "login": {
      "INVALID_USERNAME": "please enter your username",
      "INVALID_PASSWORD": "please enter your password",
      "INVALID_LOGIN": "username or password is wrong"
    }
  }
}
现在,如何从注册部分获取值INVALID_USERNAME? 我试过这个,但不起作用

$obj = json_decode(file_get_contents("en-us.json")); //file containing the json data above
echo $obj->{'INVALID_USERNAME'};
对不起,我是php和JSON的新手,提前感谢您的帮助,
Vidhu

您这样做是正确的:

$obj = json_decode(file_get_contents("en-us.json"));
要获取无效的\u用户名,您需要执行以下操作:

$obj->en_us->registration->INVALID_USERNAME

这是因为
无效\u用户名
是对象
注册
中的一个属性,它是对象
en\u us
下的一个属性,您这样做是正确的:

$obj = json_decode(file_get_contents("en-us.json"));
要获取无效的\u用户名,您需要执行以下操作:

$obj->en_us->registration->INVALID_USERNAME
这是因为
无效的\u用户名
是对象
注册
中的一个属性,它是对象
en\u us
下的一个属性。您可以尝试此属性

$obj = json_decode(file_get_contents("en-us.json")); //file containing the json data above

echo $obj->{'en-us'}->{'registration'}->{'INVALID_USERNAME'};
你可以试试这个

$obj = json_decode(file_get_contents("en-us.json")); //file containing the json data above

echo $obj->{'en-us'}->{'registration'}->{'INVALID_USERNAME'};

作为问题的解决方案,请尝试执行下面的代码段

<?php
 $json_content=file_get_contents("en-us.json");
 $json_array=json_decode($json_content,true);
 if(!empty($json_array))
 {
    echo $json_array['en-us']['registration']['INVALID_USERNAME'];
 }
?>

作为问题的解决方案,请尝试执行下面的代码片段

<?php
 $json_content=file_get_contents("en-us.json");
 $json_array=json_decode($json_content,true);
 if(!empty($json_array))
 {
    echo $json_array['en-us']['registration']['INVALID_USERNAME'];
 }
?>