Trouble with Python JSON input from STDIN

2019-07-31 04:18发布

问题:

input = json.load(sys.stdin)
print(input['id'])

When I input {"id":1} and hit enter, my program does not continue, I am just stuck typing in my input. How can I make the program continue after valid json has been passed to my stdlin?

回答1:

when you read in from sys.stdin it will read everything until it hits an EOF character normally ctrl-d so if you input {"id":1} <ENTER> ctrl-d it should work.

It looks like what you are trying to do is something like this

import json
json_as_str = input()
json_obj = json.loads(json_as_str)
print(json_obj['id'])