Encoding: Converts a Python object encoding into a JSON string
Decoding: Convert JSON format string decoding to Python object
Json.dumps for simple data types encoding
Json.loads processing decoding conversions for simple data types
Look at the example below to show clearly the difference between dumps and loads.
Import JSON Data=[{' A ': "A", ' B ':(2,4), ' C ': 3.0}]//list data type Print data Output is: [{' A ': ' A ', ' C ': 3.0, ' B ': (2, 4)}] Print len (data) Output 1 Print Data[0] Output {' A ': ' A ', ' C ': 3.0, ' B ': (2, 4)}
Coding Data_dump=json.dumps (data)//encoding Print Len (data_dump)//converted to string Output 35 Print Data_dump Output [{"A": "A", "C": 3.0, "B": [2, 4]}] Print Data_dump[0] Output: [
to decode Data_loads=json.loads (Data_dump) Print Data_loads Output: [{u ' a ': U ' a ', U ' C ': 3.0, U ' B ': [2, 4]}] Print Len (data_loads) Output 1 Print Data_loads[0] Output: {u ' a ': U ' a ', U ' C ': 3.0, U ' B ': [2, 4]} |