简体   繁体   English

使用PHP获取JSON数据

[英]Getting JSON data with PHP

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. 其中约有50个条目。 I'm trying to parse this data and store the values in variables. 我正在尝试解析这些数据并将值存储在变量中。

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? 我在这做错了什么?

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返回一个对象, json_decode使用json_decode($jsonString)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 您还可以通过将第二个参数设置为true来将您的json数据作为数组访问

$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. 更具概念性的是,PHP json_decode可以返回一个对象数组(而不仅仅是一个对象)或一个关联数组。

Consider the following json string. 考虑以下json字符串。 This string represents a "collection" (square brackets) of json data structures (curly braces). 此字符串表示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'];
}

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); 试试$json_array = json_decode($string,false); . That will return objects 这将返回对象

Also, as @MatRt mentions, you need to use $value instead of $key to reference the objects 另外,正如@MatRt所提到的,您需要使用$ value而不是$ key来引用对象

You need to retrieve values with array syntax: 您需要使用数组语法检索值:

$item['key']

as apposed to 如同

$item->key

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM