简体   繁体   中英

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.

I have a JSON file which has data like this:

   {
      "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.

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.

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 )

$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

$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.

Consider the following json string. This string represents a "collection" (square brackets) of json data structures (curly braces).

[
    {
        "name": "One"
    },
    {
        "name": "Two"
    }
]

If we assign this json to the variable $string hopefully this will illustrate the difference

$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.

Try $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

You need to retrieve values with array syntax:

$item['key']

as apposed to

$item->key

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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