简体   繁体   中英

unable to get data from json in php

I have a json that is of the following structure:

{
  "data":{
    .   .   .
    .   .   .
    .   .   .
  },
  "meta":{
    .   .   .
    .   .   .
    .   .   .
  },
  "included":[
    {
      .   .   .
      .   .   .
    },
    { .   .   . },
    { .   .   . },
    { .   .   . },
    .   .   .   .
    {
      "unwanted":{
        .   .   .
      },
      "unwanted2":{ .   .   . },
      .    .    .    .
      "wanted1":"v1"
      "wanted2":{
        "k1":"v2",
        "k2":"v3"
      },
      "wanted3":[
        "v4",
        "v5",
        "v6"
      ],
    },
    { .   .   .},
    .   .   .   .
  ],
}

What I want is to get all the wanted things. Now first I tried output the following:

$JSON = json_decode($data, true);
echo $myJSON['included'];

Now this outputs "array" showing that this is an array. I then tried:

echo $myJSON['included']['wanted1'];

which shows no index named "wanted1". I figured since all the wanted stuff is in 17th element inside included so I decided to do this:

$i=1;
foreach( $myJSON['included'] as $item ){
    if($i==17){
        echo $item['wanted1'].PHP_EOL;
    };
    $i=$i+1
};

Now this also shows the same error. I do not know how to parse this. I am new to php please help.

Edit: Here is a link to json file What I want is "foundedOn", "Specialities","name".

If you echo $myJSON['included'][16] and get "Trying to get property 'wanted1' of non-object" then you actually can access the variable with $myJSON['included'][16]['wanted1']

Note if you use PHP json_decode($data, true) it will turn your data into PHP array form

https://www.php.net/manual/en/function.json-decode.php

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