2017-06-15 67 views
1

JSON数组我有一个JSON文件是这样的:得到与json_decode

[{"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) 

我的目标是获得什么在 '分子'= >阵列

CONTROLER:

/** 
* @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()); 
} 

它适用于所有项目$为exept分子,我想:

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

和其他方式,你认为有办法让这个JSON数组什么或者我应该重新格式化JSON?

+0

'分子'本身就是一个数组,所以你需要进一步循环这个或做'$ item ['Molecule'] [0] ['Synonyms']'? –

+0

我认为我们会接近,但现在我得到了这个错误:类型错误:参数1传递给NcstoxBundle \ Entity \ JsonTextMining :: setMolecule()必须是Doctrine \ Common \ Collections \ ArrayCollection的一个实例, /jsp/json/demo/workspace/NCSTOX/src/NcstoxBundle/Controller/DefaultController.php on line 128 – Gy0m

+0

'json_decode($ json,true)[0] ['Molecule'] [0] ['Main name']' –

回答

1

试试这个:

$result = json_decode($json, true); 

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

对于分子可以让这个:

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

或本:

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

你有很多分子,所以我认为你需要很多插入

+0

我已经做了'$ tab = json_decode($ json,true);' – Gy0m

+0

,结果是....? –

+0

结果是什么在我的帖子当我var_dump $ tab – Gy0m

1

试试这个解码的JSON与json_decode($json, true)数组:

<?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"]); 
+0

我得到一个错误'注意:未定义偏移:0' – Gy0m

+0

@ Gy0m然后有些事情不是你所告诉我们的 – RiggsFolly

+0

@ Gy0m如果你只是运行这个代码我相信你不会得到任何错误,我认为你的情况是不同的请更新你的完整代码 –

1

我不能因为我的名气评论(如果我可以,我。会因为亚历山德罗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 
       (
       ) 

     ) 
... 

是不是你想要的?如果你想要每个主名,我认为你需要做一个foreach。

+0

当我把标签之前[0]它给出的错误'注意:未定义的偏移量:0'我进入Symfony – Gy0m

+0

您是否尝试过使用'print_r($ res)'?有时'var_dump'太抽象了,你不明白为什么它可能是错误的。 我用symfony工作了一下,这个语法工作。我不明白为什么它不会工作,如果有什么真正显而易见的东西,我们没有看到它。 – Yasuu

+0

print_r显示数组不为空:Array([0] => Array([id] => https://www.ncbi.nlm.nih.gov/pmc/articles/PMC102324/pdf [Annotated_file] => http://nactem10.mib.man.ac.uk/brat-v1.3/#/NCSTOX/PMC102324 [标题] =>作为昆虫熏蒸剂的硫代葡萄糖苷分解产物及其对二氧化碳排放的影响[分子] => Array([0] => Array([Main name] => glucosinolate [Synonyms => Array())[1] => – Gy0m