繁体   English   中英

用json_decode获取json数组

[英]get json array with json_decode

我有一个像这样的json文件:

  [{"id":"PMC102324",
"Original_paper":"https://www.ncbi.nlm.nih.gov/pmc/articles/PMC102324/pdf",
"Annotated_file":"http://nactem10.mib.man.ac.uk/brat-v1.3/#/NCSTOX/PMC102324",
"Title":"Glucosinolate breakdown products as insect fumigants and their effect on carbon dioxide emission of insects",
"Molecule":[{"Main name":"glucosinolate", "Synonyms":[]},{"Mainame":"isothiocyanate", "Synonyms":[]},{"Main name":"hexane", "Synonyms":    []},{"Main name":"sinigrin", "Synonyms":[]},{"Main name":"allyl glucosinolate", "Synonyms":[]},{"Main name":"rotenone", "Synonyms":[]},{"Main name":"sucrose", "Synonyms":[]},{"Main name":"thiocyanate", "Synonyms":[]},{"Main name":"allyl isothiocyanate", "Synonyms":[]}],
"ToxKeywords":"safety, cytotoxic, ",
"Important_sentences":["The mode of action of many isothiocyanate compounds has also been attributed to their capability for alkylating the nucleophilic groups of biopolymers such as DNA, thus having cytotoxic properties which can affect the formation of the spiracular epidermis and crochet on the prolegs of tobacco hornworm (Manduca sexta L.) caterpillars [28-30]."]
}]

当我使用json_decodevar_dump()返回:

 array (size=1)
 0 => 
array (size=7)
  'id' => string 'PMC102324' (length=9)
  'Original_paper' => string 'https://www.ncbi.nlm.nih.gov/pmc/articles/PMC102324/pdf' (length=55)
  'Annotated_file' => string 'http://nactem10.mib.man.ac.uk/brat-v1.3/#/NCSTOX/PMC102324' (length=58)
  'Title' => string 'Glucosinolate breakdown products as insect fumigants and their effect on carbon dioxide emission of insects' (length=107)
  'Molecule' => 
    array (size=9)
      0 => 
        array (size=2)
          ...
      1 => 
        array (size=2)
          ...
      2 => 
        array (size=2)
          ...
      3 => 
        array (size=2)
          ...
      4 => 
        array (size=2)
          ...
      5 => 
        array (size=2)
          ...
      6 => 
        array (size=2)
          ...
      7 => 
        array (size=2)
          ...
      8 => 
        array (size=2)
          ...
  'ToxKeywords' => string 'safety, cytotoxic, ' (length=19)
  'Important_sentences' => 
    array (size=1)
      0 => string 'The mode of action of many isothiocyanate compounds has also been attributed to their capability for alkylating the nucleophilic groups of biopolymers such as DNA, thus having cytotoxic properties which can affect the formation of the spiracular epidermis and crochet on the prolegs of tobacco hornworm (Manduca sexta L.) caterpillars [28-30].' (length=343)

我的目标是获得'Molecule'=>数组中的内容

Contrôler:

 /**
 * @Route("/parse_file", name="parseFile")
 * @method("GET")
 */
public function parseFile()
{
    $em = $this->getDoctrine()->getManager();
    $em->getRepository('NcstoxBundle:JsonTextMining');

    set_include_path('/home/landreau/workspace/NCSTOX/web/assets/json/sample-json');
    $json = file_get_contents('PMC102324.json', FILE_USE_INCLUDE_PATH );
    $tab = json_decode($json, true);

    var_dump($json);
    var_dump($tab);

    foreach ($tab as $item) {
        $jsonTextMining = new JsonTextMining();
        $jsonTextMining->setSolrId($item['id']);
        $jsonTextMining->setOriginalPaper($item['Original_paper']);
        $jsonTextMining->setAnnotatedFile($item['Annotated_file'][0]);
        $jsonTextMining->setTitle($item['Title'][0]);
        $jsonTextMining->setMolecule($item['Molecule']['Main name']);
        $jsonTextMining->setMolecule($item['Molecule']['Synonyms']);
        $jsonTextMining->setKeyword($item['ToxKeywords'][0]);
        $jsonTextMining->setImportantSentence($item['Important_sentences'][0]);


        $em = $this->getDoctrine()->getManager();
        $em->persist($jsonTextMining);
    }

    $em->flush();


    return new Response('Saved new document with id ' . $jsonTextMining->getSolrId());
}

它适用于Molecule所有$项目,我试过:

            $jsonTextMining->setMolecule($item['Molecule']['Main name'][0]);

和其他方式你认为有一种方法可以获得这个json数组中的内容或者我应该重新格式化json吗?

尝试这个:

$result = json_decode($json, true);

如果使用true作为第二个参数, json_decode创建一个关联数组

对于Molecule,您可以这样做:

$item['Molecule'][0]['Main name']

或这个:

$item[0]['Molecule'][0]['Main name']

你有很多Molecule所以我认为你需要很多插入

尝试使用json_decode($json, true)对数组解码json:

<?php
    $json = '[{"id":"PMC102324",
    "Original_paper":"https://www.ncbi.nlm.nih.gov/pmc/articles/PMC102324/pdf",
    "Annotated_file":"http://nactem10.mib.man.ac.uk/brat-v1.3/#/NCSTOX/PMC102324",
    "Title":"Glucosinolate breakdown products as insect fumigants and their effect on carbon dioxide emission of insects",
    "Molecule":[{"Main name":"glucosinolate", "Synonyms":[]},{"Mainame":"isothiocyanate", "Synonyms":[]},{"Main name":"hexane", "Synonyms":    []},{"Main name":"sinigrin", "Synonyms":[]},{"Main name":"allyl glucosinolate", "Synonyms":[]},{"Main name":"rotenone", "Synonyms":[]},{"Main name":"sucrose", "Synonyms":[]},{"Main name":"thiocyanate", "Synonyms":[]},{"Main name":"allyl isothiocyanate", "Synonyms":[]}],
    "ToxKeywords":"safety, cytotoxic, ",
    "Important_sentences":["The mode of action of many isothiocyanate compounds has also been attributed to their capability for alkylating the nucleophilic groups of biopolymers such as DNA, thus having cytotoxic properties which can affect the formation of the spiracular epidermis and crochet on the prolegs of tobacco hornworm (Manduca sexta L.) caterpillars [28-30]."]
    }]';
    $array = json_decode($json, true);
    echo "<pre>";
    print_r($array[0]["Molecule"]);

由于我的声誉,我无法评论(如果可以的话,我会因为Alessandro Minoccheri的答案看起来很好。)

我做了亚历山德罗所做的事,它对我有用:

$res = json_decode($json, true);
print_r($res[0]['Molecule']);

给我那个:

Array
(
    [0] => Array
        (
            [Main name] => glucosinolate
            [Synonyms] => Array
                (
                )

        )

    [1] => Array
        (
            [Mainame] => isothiocyanate
            [Synonyms] => Array
                (
                )

        )
...

这不是你想要的吗? 如果你想要每个Mainname,我想你需要做一个foreach。

暂无
暂无

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

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