使用 python 读取 json 文件

read_json.json:

{
    "rule":{
        "namespace":"strategy",
        "name":"test_exp_1496234234223400",
        "version":0,
        "last_modify_time":1434234236819000,
        "log_rate":1023300,
        "schema_version":"hello_world!"
    }
}

read_json.py:

# -*- coding:utf-8 -*-
import json

with open("read_json.json", 'r') as f:
    temp = json.loads(f.read())
    print(temp)
    print(temp['rule'])
    print(temp['rule']['namespace'])

输出:

{'rule': {'name': 'test_exp_1496234234223400', 'log_rate': 1023300, 'namespace': 'strategy', 'schema_version': 'hello_world!', 'last_modify_time': 1434234236819000L, 'version': 0}}

{'name': 'test_exp_1496234234223400', 'log_rate': 1023300, 'namespace': 'strategy', 'schema_version': 'hello_world!', 'last_modify_time': 1434234236819000L, 'version': 0}
strategy

Process finished with exit code 0

你可能感兴趣的:(Python基础)