我有一个小的PHP代码,试图读取.json文件,提取内容然后将其转换为数组。
相反,我得到一个字符串。
json.file是在下面的python代码中创建的。
python脚本
dict_test= {'Subcellular': ['Ribosome', 'Plasma Membrane'], 'CAS': ['56-85-9', '50-99-7'], 'Bio_target': ['RNA 18S', 'Insuline receptor'], 'strain': ['', ''], 'Unity': ['J', 'nM'], 'value': ['-80', '0.01'], 'InchI': ['1S/C5H10N2O3/c6-3(5(9)10)1-2-4(7)8/h3H,1-2,6H2,(H2,7,8)(H,9,10)\\xa0', '1S/C6H12O6/c7-1-2-3(8)4(9)5(10)6(11)12-2/h2-11H,1H2/t2-,3-,4+,5-,6?/m1/s1\\xa0'], 'Conditions': ['Temperature of 25\\xbaC and normal pressure', 'Computational simulation in R module canislup'], 'Link\\n': ['www.soulink.pt\\n', 'www.sououtro.pt\\n'], 'Asay_parameter': ['enthalpy', 'concentration'], 'Smiles': ['O=C(N)CCC(N)C(=O)O', 'OC[C@H]1OC(O)[C@H](O)[C@@H](O)[C@@H]1O'], 'Journal': ['Nature', 'Science'], 'Experimental_error': ['0.01 J', '0.05 uM'], 'Title': ['Glutamine is a novel compound in Brainstem studies', 'Glucose concetration is essential to calcium inducted waves'], 'Assay_ID': ['12345', '123456'], 'Cell_type': ['Neuron', 'Myocite'], 'Comparisons': ['enthalpy>70 J', 'Concentration< 10 uM'], 'Mol_name': ['Glutamine', 'Glucose'], 'target_type': ['Peptide', 'Protein'], 'LAB': ['IMED', 'Lasige'], 'Tissue': ['Brainstem', 'Pericardium'], 'Species': ['Homo sapiens', 'Canis lupus'], 'Observations': ['Outliers were not found', 'Python modules were also used']}
utf=unicode(dic_test)
output_file= 'aaa.txt'+'.json'
import json
with open(output_file, 'wb') as fp:
json.dump(utf, fp)
.json文件
"{'Subcellular': ['Ribosome', 'Plasma Membrane'], 'CAS': ['56-85-9', '50-99-7'], 'Bio_target': ['RNA 18S', 'Insuline receptor'], 'strain': ['', ''], 'Unity': ['J', 'nM'], 'value': ['-80', '0.01'], 'InchI': ['1S/C5H10N2O3/c6-3(5(9)10)1-2-4(7)8/h3H,1-2,6H2,(H2,7,8)(H,9,10)\\xa0', '1S/C6H12O6/c7-1-2-3(8)4(9)5(10)6(11)12-2/h2-11H,1H2/t2-,3-,4+,5-,6?/m1/s1\\xa0'], 'Conditions': ['Temperature of 25\\xbaC and normal pressure', 'Computational simulation in R module canislup'], 'Link\\n': ['www.soulink.pt\\n', 'www.sououtro.pt\\n'], 'Asay_parameter': ['enthalpy', 'concentration'], 'Smiles': ['O=C(N)CCC(N)C(=O)O', 'OC[C@H]1OC(O)[C@H](O)[C@@H](O)[C@@H]1O'], 'Journal': ['Nature', 'Science'], 'Experimental_error': ['0.01 J', '0.05 uM'], 'Title': ['Glutamine is a novel compound in Brainstem studies', 'Glucose concetration is essential to calcium inducted waves'], 'Assay_ID': ['12345', '123456'], 'Cell_type': ['Neuron', 'Myocite'], 'Comparisons': ['enthalpy>70 J', 'Concentration< 10 uM'], 'Mol_name': ['Glutamine', 'Glucose'], 'target_type': ['Peptide', 'Protein'], 'LAB': ['IMED', 'Lasige'], 'Tissue': ['Brainstem', 'Pericardium'], 'Species': ['Homo sapiens', 'Canis lupus'], 'Observations': ['Outliers were not found', 'Python modules were also used']}"
php
<?php
$string = file_get_contents("aaa.txt.json");
$json = json_decode($string, true);
var_dump($json);
?>
php的输出是一个字符串。
答案 0 :(得分:0)
来自php文档:
//名称和值必须用双引号括起来 //单引号无效
从您的python脚本创建的Json无效。您需要对名称使用双引号并删除链接中的// n
{"Subcellular": ["Ribosome", "Plasma Membrane"], "CAS": ["56-85-9", "50-99-7"], "Link": ["www.soulink.pt", "www.sououtro.pt"], "Bio_target": ["RNA 18S", "Insuline receptor"], "strain": ["", ""], "Unity": ["J", "nM"], "LAB": ["IMED", "Lasige"], "InchI": ["1S/C5H10N2O3/c6-3(5(9)10)1-2-4(7)8/h3H,1-2,6H2,(H2,7,8)(H,9,10)\\\\xa0", "1S/C6H12O6/c7-1-2-3(8)4(9)5(10)6(11)12-2/h2-11H,1H2/t2-,3-,4+,5-,6?/m1/s1\\\\xa0"], "Conditions": ["Temperature of 25\\\\xbaC and normal pressure", "Computational simulation in R module canislup"], "Asay_parameter": ["enthalpy", "concentration"], "Smiles": ["O=C(N)CCC(N)C(=O)O", "OC[C@H]1OC(O)[C@H](O)[C@@H](O)[C@@H]1O"], "Journal": ["Nature", "Science"], "Title": ["Glutamine is a novel compound in Brainstem studies", "Glucose concetration is essential to calcium inducted waves"], "Experimental_error": ["0.01 J", "0.05 uM"], "Assay_ID": ["12345", "123456"], "Cell_type": ["Neuron", "Myocite"], "Comparisons": ["enthalpy>70 J", "Concentration< 10 uM"], "Mol_name": ["Glutamine", "Glucose"], "target_type": ["Peptide", "Protein"], "value": ["-80", "0.01"], "Tissue": ["Brainstem", "Pericardium"], "Species": ["Homo sapiens", "Canis lupus"], "Observations": ["Outliers were not found", "Python modules were also used"]}
如上所述更改你的Json之后你的php就可以了。
$string = file_get_contents("aaa.txt.json");
$json = json_decode($string, true);
echo "<pre>";
print_r($json);
echo "</pre>";