使用 PHP 获取 JSON 数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16578019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting JSON data with PHP
提问by JVG
Apologies if this has been asked a thousand times, but I can't find a good tutorial on how to do this correctly and searching on Stack is coming up trumps.
如果这已经被问了一千次,我很抱歉,但我找不到关于如何正确执行此操作的好教程,并且在 Stack 上搜索将成为王牌。
I have a JSON file which has data like this:
我有一个 JSON 文件,其中包含如下数据:
{
"store":"Store 1",
"cat":"Categories",
"general_cat":"Categories",
"spec_cat":"Accessories"
},
{
"store":"Store 1",
"cat":"Categories",
"general_cat":"Categories",
"spec_cat":"Accessories"
},
with about 50 entries in it. I'm trying to parse this data and store the values in variables.
大约有 50 个条目。我正在尝试解析这些数据并将值存储在变量中。
So far I've tried:
到目前为止,我已经尝试过:
$string = file_get_contents("jsonFile.json");
$json_array = json_decode($string,true);
foreach ($json_array as $key => $value){
$store = $key -> store;
$general_cat = $key -> general_cat;
$spec_cat = $key -> spec_cat;
if (!is_null($key -> mainImg_select)){
$cat = $key -> cat;
}
echo $headURL;
}
This is resulting in "Trying to get property of non object" errors. What am I doing wrong here?
这导致“试图获取非对象的属性”错误。我在这里做错了什么?
回答by Pete Mitchell
The second argument of json_decode
tells the function whether to return the data as an object, or an array.
的第二个参数json_decode
告诉函数是将数据作为对象还是作为数组返回。
Object access uses the ->
symbol. To return an object from json_decode
either use json_decode($jsonString)
or json_decode($jsonString, false)
(the second argument is false by default)
对象访问使用->
符号。从json_decode
use json_decode($jsonString)
or返回一个对象json_decode($jsonString, false)
(第二个参数默认为false)
$jsonString = '{ "this_is_json" : "hello!" }';
$obj = json_decode($jsonString);
echo $obj->this_is_json // "hello!";
You can also access your json data as an array by setting the second argument to true
您还可以通过将第二个参数设置为数组来访问 json 数据 true
$jsonString = '{ "this_is_json" : "hello!" }';
$arr = json_decode($jsonString, true);
echo $arr['this_is_json'] // "hello!";
What can be a little more conceptually confusing, is that PHP json_decode
can return either an array of objects (rather than just an object), or an associative array.
在概念上更令人困惑的是,PHPjson_decode
可以返回一个对象数组(而不仅仅是一个对象),或者一个关联数组。
Consider the following json string. This string represents a "collection" (square brackets) of json data structures (curly braces).
考虑以下 json 字符串。此字符串表示 json 数据结构(花括号)的“集合”(方括号)。
[
{
"name": "One"
},
{
"name": "Two"
}
]
If we assign this json to the variable $string
hopefully this will illustrate the difference
如果我们将此 json 分配给变量,$string
希望这将说明差异
$asObjects = json_decode($string);
$asAssociativeArray = json_decode($string, true);
foreach ($asObjects as $obj) {
echo $obj->name;
}
foreach ($asAssociativeArray as $arr) {
echo $arr['name'];
}
回答by andreshernandez
It seems like you are requesting an associative array (by passing True as the second parameter to the json_decode function) but trying to use it as an object.
似乎您正在请求关联数组(通过将 True 作为第二个参数传递给 json_decode 函数),但试图将其用作对象。
Try $json_array = json_decode($string,false);
. That will return objects
试试$json_array = json_decode($string,false);
。这将返回对象
Also, as @MatRt mentions, you need to use $value instead of $key to reference the objects
此外,正如@MatRt 提到的,您需要使用 $value 而不是 $key 来引用对象
回答by DeweyOx
You need to retrieve values with array syntax:
您需要使用数组语法检索值:
$item['key']
as apposed to
相对于
$item->key