doushi1974 2014-12-11 12:04
浏览 52
已采纳

如何解码从PHP传递的编码数组

I have problem in decoding the array passed from PHP. My PHP code is

$checkedJson = json_encode($dynamic_species);
$tmp = exec("/Python33/arr_pass.py $pressure $temp $checkedJson");
return $tmp;

If i print $checkedJson i get

{"species1":"CH4","species2":"C2H6"} as print statement

My python code is

species_list = sys.argv[3]
species_list_data = json.loads(species_list)
print(species_list_data['species1'])

This python script returns empty string as output to php

I am working for first time on JSON can anyone please help me.

Thanks in advance

  • 写回答

1条回答 默认 最新

  • doujiayuan8415 2014-12-11 12:13
    关注

    This is neither a JSON nor Python question. What you want is to figure out how to get output back when your PHP program runs something via exec(). Basically you're lacking the output parameter; RTM at php.net. You should try:

    exec("/Python33/arr_pass.py $pressure $temp $checkedJson", $output);
    

    After which $output will be an array of lines that your exec'd script sent to its output. In your code, $tmp is assigned to the last line that the exec'd thing prints, so probably an empty line.

    Plus you have one more challenge; the way you pass $checkdJson to the Python script will fail. You will have to quote it to make it one commandline parameter, so you'll probably need

    exec("/Python33/arr_pass.py $pressure $temp '$checkedJson'", $output);
    

    (note the extra single quotes).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?