PHP使用JSON获取数据
我目前正在尝试本地化我的网站,所有数据都将存储在JSON文件中。所以我想知道如何使用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
{
"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'];
}
?>