python json file retrieval operation content

When writing case, write the json file the case than written, write python must learn to deal with json

 

The following is to be processed json

The processing operations include: open json file, acquires the file contents json, json close the file, read the contents of the corresponding key value

 

{
    "name": "BeJson",
    "url": "http://www.bejson.com",
    "page": 88,
    "isNonProfit": true,
    "address": {
        "street": "科技园路.",
        "city": "江苏苏州",
        "country": "中国"
    },
    "links": [
        {
            "name": "Google",
            "url": "http://www.google.com"
        },
        {
            "name": "Baidu",
            "url": "http://www.baidu.com"
        },
        {
            "name": "SoSo",
            "url": "http://www.SoSo.com"
        }
    ]
}

python implementation:

#coding=utf-8
import json

class OperationJson:
    def __init__(self,file_name=None):    
        if file_name:
            self.file_name = file_name
        else:
            self.file_name = './dataConfig/data.json'
        self.data = self.get_data()
        
    def get_data(self):
        fp = open(self.file_name)
        data = json.load(fp)
        fp.close()
        return data
    
    def get_value(self,id):
        return self.data[id]

if __name__ == '__main__': opers = OperationJson() print opers.get_value('name')

Guess you like

Origin www.cnblogs.com/ansonwan/p/12077179.html
Recommended