2015-03-15 3 views
0

У меня есть небольшой PHP-код, который пытается прочитать .json-файл, извлечь содержимое и затем преобразовать его в массив.json_decode создает строку

Вместо этого я получаю строку.

Файл json.file создан в коде 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[[email protected]]1OC(O)[[email protected]](O)[[email protected]@H](O)[[email protected]@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[[email protected]]1OC(O)[[email protected]](O)[[email protected]@H](O)[[email protected]@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 является строка.

+2

Вы дали строку 'json.dump()', так что вы ожидали? В чем причина наличия неиспользуемого 'dict_test', btw? Кроме того, какое поведение удивляет вас, Python или PHP? Если вы уменьшили свой пример до минимального количества кода, как того требует руководство, это даже не вопрос. –

ответ

0

Из документации PHP:

// имя и значение должно быть заключено в двойные кавычки
// одиночные кавычки не являются действительными

Json создан из вашего питона сценария инвалид. Вы должны использовать двойные кавычки для имен и вырезать // п внутри звеньев

{"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[[email protected]]1OC(O)[[email protected]](O)[[email protected]@H](O)[[email protected]@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, как указано выше РНР будет работать.

$string = file_get_contents("aaa.txt.json"); 
$json = json_decode($string, true); 
echo "<pre>"; 
print_r($json); 
echo "</pre>"; 

enter image description here

Смежные вопросы