json data conversion of python notes

'''
json data processing

'''

import json

test = [{"a":1,"aa":11,"aaa":111},{"b":2,"bb":22,"bbb":222},{"c":3,"cc":33,"ccc":333}]
print(type(test)) #json data is list
print(test[0]["a"])

test = '''[{"a":1,"aa":11,"aaa":111},{"b":2,"bb":22,"bbb":222},{"c":3,"cc":33,"ccc":333}]'''
print(type(test)) # The json data of test is str
# json.dumps()
xtest = json.loads(test) # It can be used as soon as it is loaded ----- json of test is st to list
print(type(xtest)) # The json data of test is list
print(xtest[0]["a"])

# test2 = '''{"aaa":111,"bbb":222}'''
# xtest = json.load(test2)
# print(type(xtest))
# print(xtest)

'''
Use json to convert between str and list without error
'''
print(xtest)
xxx = str(xtest) # convert to string
print(xxx)
# print(type(json.load(xxx))) #Error, can't do this

yyy = json.dumps(xtest) # convert to string
print(type(yyy))
print(type(json.load(yyy))) # This can also convert the string to a list again

# print(type(json.load(x)))
# print(type(json.load(y)))

'''
Standard strings are "double quotes, which are used across languages
'''
test = '''{'aa':11,'bb':22}''' #This is not possible

'''
json module
dict              object
list,tuple        array
str,unicode       string
int,long,float    number
True              true
false false
None              null

dumps/loads with s is a string
Dump/load without s is to process files

'''

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325289545&siteId=291194637