简体   繁体   English

无法从php中的json获取数据

[英]unable to get data from json in php

I have a json that is of the following structure:我有一个具有以下结构的json:

{
  "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.现在输出“array”,表明这是一个数组。 I then tried:然后我尝试:

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

which shows no index named "wanted1".它显示没有名为“wanted1”的索引。 I figured since all the wanted stuff is in 17th element inside included so I decided to do this:我想,因为所有想要的东西都在里面的第 17 个元素中,所以我决定这样做:

$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.我是 php 新手,请帮忙。

Edit: Here is a link to json file What I want is "foundedOn", "Specialities","name".编辑: 这是 json 文件的链接我想要的是“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']如果您回显 $myJSON['included'][16] 并得到“试图获取非对象的属性 'wanted1'”,那么您实际上可以使用 $myJSON['included'][16]['wanted1' 访问该变量]

Note if you use PHP json_decode($data, true) it will turn your data into PHP array form请注意,如果您使用 PHP json_decode($data, true) 它会将您的数据转换为 PHP 数组形式

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

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

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