简体   繁体   中英

how to read json file without using json libarary or without any other libarary in to dictionary or list?

I have a JSON file and i need to read it into dictionary or list without using and library.This is my file content.

{
   "101":"Break and Enter Commercial",
   "102":"Break and Enter Residential/Other",
   "103":"Vehicle Collision or Pedestrian Struck (with Fatality)",
   "104":"Vehicle Collision or Pedestrian Struck (with Injury)"
}

This is what i try

def read_codes(filename):
    jsonData = {}
    # empty list to append to it later
    file = open(filename, "r")
    for key in file:
        print(key)
    return jsonData
print(read_codes('codes.json'))

What about such way:

with open(file) as f:
    your_dict = eval(f.read().replace('\n', ''))

You could open it as a text file. It would return you list, then filter the list as you require.

with open('file.json', 'r') as jsonFile:
    json_obj = jsonFile.readlines()
json_obj = [(obj.rstrip()).lstrip()[:-1] for obj in json_obj[1:-1]]
print(json_obj)

I have a JSON file and i need to read it into dictionary or list without using and library.This is my file content.

{
   "101":"Break and Enter Commercial",
   "102":"Break and Enter Residential/Other",
   "103":"Vehicle Collision or Pedestrian Struck (with Fatality)",
   "104":"Vehicle Collision or Pedestrian Struck (with Injury)"
}

This is what i try

def read_codes(filename):
    jsonData = {}
    # empty list to append to it later
    file = open(filename, "r")
    for key in file:
        print(key)
    return jsonData
print(read_codes('codes.json'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM