Python reads the Android permission file

  • 2020-04-02 09:53:43
  • OfStack

Today, I used python to parse a text file in the following format:


[
    {
        "Key":"android.permission.ACCESS_CHECKIN_PROPERTIES",
        "Title":" Access the check-in property ",
        "Memo":" Allows you to read properties uploaded by the check-in service / Write access. Normal applications cannot use this permission. ",
        "Level":0
    },
    {
        "Key":"android.permission.ACCESS_COARSE_LOCATION",
        "Title":" General location ",
        "Memo":" Access the approximate location source ( For example, a cellular network database ) To determine the approximate location of the phone ( If you can ) . A malicious application can use this to determine your approximate location. ",
        "Level":1
    },
    {
        "Key":"android.permission.ACCESS_COARSE_LOCATION",
        "Title":" General location ",
        "Memo":" Access the approximate location source ( For example, a cellular network database ) To determine the approximate location of the phone ( If you can ) . A malicious application can use this to determine your approximate location. ",
        "Level":1
    }
]

I started with the open('filepath').readlines() method, so that everything I read was accessed to a list, but I couldn't get the contents of each {}. Afterwards consulted netizen, say to use json way to read. Therefore, the following methods are adopted:

#-*-encoding:utf-8-*-
import json
f = file(r'C:UsersTimDesktoptest.json')
jsonobj = json.load(f)
# The list is queried with an ordinal number 
print jsonobj[0]['Memo']
f.close

Report the following error after operation:
ValueError: No JSON object could be decoded
Save the json file in UTF8 without BOM, and run successfully. Alternatively, you can read a json string in the following way:

s = json.loads('{"name":"test", "type":{"name":"seq", "parameter":["1", "2"]}}')
print s
print s.keys()
print s["name"]
print s["type"]["name"]
print s["type"]["parameter"][1]

The complete code to read the permission file is given below:

#-*-coding:utf8-*-
import json
import codecs
def getperinfo(inputper):
    f = file(r'C:UsersTimDesktoptest.json')
    jsonobj = json.load(f)
    #print jsonobj[0]['Memo']
    for permission in jsonobj:
        #permission.values() Gets the value of the dictionary 
        if permission.values()[2] == inputper:
            print "permission name:%snpermission info:%s" %(permission.values()[3],permission.values()[0])
    f.close
if __name__ == '__main__':
    #optparse Using predefined options to parse command line arguments, optparse The default is to parse command-line arguments. 
    from optparse import OptionParser
    parser = OptionParser()
    parser.add_option("-p", "--permission", dest="permission",help="input permission")
    (options, args) = parser.parse_args()
    #options.permission For the input of permission
    getperinfo(options.permission)

The execution example is as follows:
< img SRC = "border = 0 / / files.jb51.net/file_images/article/201311/20131101103549.jpg? 2013101103627 ">


Related articles: