2

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?

3
  • 1
    First, your json code is not semantically correct. A list of object should be between [ and ] like [{...},{...}]. Secondly, you are using the value true for the second parameter of json_decode. If it is set to true, you will not manipulate object, but associative arrays. So, $key->store will not be available. At the same time it may be instead $value["store"].
    – MatRt
    Commented May 16, 2013 at 2:30
  • The JSON is between square brackets, I was just showing how the data is stored. I've tried setting json_decode's second value to false but this still gives me non-object errors.
    – JVG
    Commented May 16, 2013 at 2:32
  • 1
    That's because you have to use $value and not $key. Please read completely my answer. After json_decode, $json_array is an array of associative arrays. So, $key is a numeric index, and $value the array you wat to manipulate.
    – MatRt
    Commented May 16, 2013 at 2:33

3 Answers 3

12

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'];
}
0
4

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

3

You need to retrieve values with array syntax:

$item['key']

as apposed to

$item->key
2
  • This is getting rid of the errors, but the variables are empty - when I use ` $store = $key['store'];` then try print_r($store); I get a blank screen
    – JVG
    Commented May 16, 2013 at 2:36
  • try foreach($json_array as $key)
    – DeweyOx
    Commented May 16, 2013 at 2:38

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.