To import the JSON module in Python3 and then use DIR (JSON) to see the functions provided by the JSON module, select a few common JSON processing functions to see the usage:
>>> Import JSON
>>> dir (JSON)
[' Jsondecodeerror ', ' jsondecoder ', ' jsonencoder ', ' __all__ ',
' __author__ ', ' __builtins__ ', ' __cached__ ', ' __doc__ ', ' __file__ ',
' __loader__ ', ' __name__ ', ' __package__ ', ' __path__ ', ' __spec__ ',
' __version__ ', ' _default_decoder ', ' _default_encoder ', ' decoder ',
' Dump ', ' dumps ', ' encoder ', ' load ', ' loads ', ' scanner '
Json.dumps
First define a list and then convert the output to see the results:
Import JSON
data = [{"A": "AAA", "B": "BBB", "C": [1,2,3, (4,5,6)]},33, ' Tantengvip ', True]
Data2 = json.dumps (data)
Print (DATA2)
Output results:
[{"C": [1, 2, 3, [4, 5, 6]], "a": "AAA", "B": "BBB"}, "Tantengvip", true]
In fact Python's list data structure is similar to the JSON data result, the conversion is largely unchanged, but true becomes true, and the 4,5,6 type is [4,5,6].
JSON |
PYTHON |
Object |
Dict |
Array |
List |
String |
Unicode |
Number (int) |
int, long |
Number (real) |
Float |
True |
True |
False |
False |
Null |
None |
This table shows the conversion differences between Python and JSON types.
Json.dump
This method uses relatively few, if direct dump (Json_data) will be an error, as follows:
data = [{"A": "AAA", "B": "BBB", "C": [1,2,3, (4,5,6)]},33, ' Tantengvip ', True]
Data2 = json.dump (data)
#TypeError: Dump () Missing 1 required positional argument: ' FP '
Error: Typeerror:dump () Missing 1 required positional argument: ' FP '
Json.dump and Json.dumps are very different, json.dump are used primarily for JSON file reading and writing, and Json.load functions. Json.dump (X,F), X is an object, F is a file object that can write a JSON string to a text file.
Import JSON
data = [{"A": "AAA", "B": "BBB", "C": [1,2,3, (4,5,6)]},33, ' Tantengvip ', True]
Data2 = json.dumps (data)
Print (DATA2)
f = open ('./tt.txt ', ' a ')
Json.dump (DATA2,F)
This generates a Tt.txt file that preserves the JSON-formatted data. Dumps also provides pritty print, formatted output.
Json.load Load JSON format file
f = open ('./tt.txt ', ' R ')
hehe = Json.load (f)
Print (hehe)
This reads the JSON data from the TXT file.
Json.lodas function
So what's the difference between the Json.loads function and json.load? As with dumps and dump, with S is the operation of the file.
Hehe2 = Json.loads (' ["AAA", {"name": "Pony"}] ")
Print (HEHE2)
Loads can directly pass JSON-formatted data as a parameter.
Look at an example of reading the weather
Import OS, io, sys, RE, time, Base64, JSON
Import WebBrowser, Urllib.request
def main ():
"Main function"
url = "Http://m.weather.com.cn/data/101010100.html"
Stdout = urllib.request.urlopen (URL)
Weatherinfo = Stdout.read (). Decode ('Utf-8')
#print (Weatherinfo)
Jsondata = Json.loads (weatherinfo)
#Output JSON data
szcity = jsondata ["Weatherinfo" ["City"]
Print ("City:", szcity)
sztemp = jsondata ["Weatherinfo"] ["Temp1"]
Print ("Temperature:", sztemp)
szWeather1 = jsondata ["Weatherinfo"] ["Weather1"]
Print ("Weather conditions:", szWeather1)
Szcityid = jsondata ["Weatherinfo" ["Cityid"]
Print ("City Code:", Szcityid)
If __name__ = = '__main__':
main ()