Trouble with Python JSON input from STDIN

2019-07-31 04:37发布

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条回答
Summer. ? 凉城
2楼-- · 2019-07-31 05:22

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'])
查看更多
登录 后发表回答