[英]How to access string object in array of JSON-array via PHP?
I have searched for almost 30 minutes and still can't find the answer for my problem.我已经搜索了将近 30 分钟,但仍然找不到我的问题的答案。 So, here is it: I have an JSON-file called "localeDE.l", now I'm trying to print the objects to the website, "locale_name"(type: string) works, but "translations"(type: array) won't work.
所以,就是这样:我有一个名为“localeDE.l”的 JSON 文件,现在我正在尝试将对象打印到网站,“locale_name”(类型:字符串)有效,但“翻译”(类型:数组) 不起作用。
My JSON file:我的 JSON 文件:
"locale_name": "DE",
"translations": [
{"Welcome": "Willkommen",
"Goodbye": "Auf Wiedersehen"}
]
Here my PHP file:这是我的 PHP 文件:
$file = file_get_contents('localeDE.l');
$locale = json_decode($file);
print_r($locale);
echo "Locale=" . $locale->{'locale_name'};
echo "Translations:";
echo " Welcome:" . $locale->{'translations'}->{'Welcome'};
echo " Goodbye:" . $locale->{'translations'}->{'Goodbye'};
I also tried something like (...) $locale->{'translations.Welcome'};
我也尝试过类似
(...) $locale->{'translations.Welcome'};
etc. Can You help me?等你能帮我吗?
- Felipe Kaiser - 费利佩·凯泽
First the JSON like you typed it here is incomplete.首先,您在此处输入的 JSON 是不完整的。 It's missing both opening and closing curly braces.
它缺少左花括号和右花括号。
It should be它应该是
{"locale_name": "DE",
"translations": [
{"Welcome": "Willkommen",
"Goodbye": "Auf Wiedersehen"}
]}
The php to read it读取它的php
$obj = json_decode($json, true);
//to read the locale name
echo $obj['locale_name'];
//to read the translation of welcome
echo $obj['tranlations'][0]['Welcome'];
//to read the tranlation of goodbye
echo $obj['translations'][0]['Goodbye'];
Cheers.干杯。
Now I figured it out how it works!现在我知道它是如何工作的了! Thanks alot to you!
非常感谢你!
Here are my code pieces:这是我的代码片段:
$json = file_get_contents('localeDE.l');
$obj = json_decode($json, true);
echo $obj['locale_name'];
echo $obj['translations'][0]['Welcome'];
echo $obj['translations'][0]['Goodbye'];
And my JSON file is unrelevant, for those who are interested, see the answer of Makville above.和我的JSON文件无关,有兴趣的可以看上面Makville的回答。
Thanks alot!非常感谢! :)
:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.