100 likes | 215 Views
COMPSCI 107 Computer Science Fundamentals. Lecture 09 – JSON. Recap. Classes are defined using the syntax: Classes contain methods that define the behaviour and operations on objects of that class We override the default behaviours for built-in methods/operations Additional Resources
E N D
COMPSCI 107Computer Science Fundamentals Lecture 09 – JSON
Recap Classes are defined using the syntax: Classes contain methods that define the behaviour and operations on objects of that class We override the default behaviours for built-in methods/operations Additional Resources http://interactivepython.org/runestone/static/thinkcspy/Classes/fractions.html class name_of_the_class: definition of the class goes here COMPSCI 107 - Computer Science Fundamentals
Exercise • What is the output of the following code? x = Fraction(2, 3) y = Fraction(1, 3) z = y + y print(x == z) print(x is z) w = x + y print(w == 1) COMPSCI 107 - Computer Science Fundamentals
Learning outcomes • At the end of this lecture, students should be able to: • understand what JSON is used for • recognise information in JSON format • use the Python JSON library to read and write standard Python data types COMPSCI 107 - Computer Science Fundamentals
JavaScript Object Notation • Text-based notation for data interchange • Human readable • Object • Unordered set of name-value pairs • { name1 : value1, name2 : value2, …, nameN : valueN } • Array • Ordered list of values • [ value1, value2, … valueN ] COMPSCI 107 - Computer Science Fundamentals
Writing JSON using Python • json.dumps( data ) • Accepts Python object as an argument • Returns a string containing the information in JSON format • Typically write this string to a file def write(data, filename): file = open(filename, 'w') str_out = json.dumps(data) file.write(str_out) file.close() COMPSCI 107 - Computer Science Fundamentals
Reading JSON using Python • json.loads( data ) • Accepts string as an argument • The string should be in JSON format • Returns a Python object corresponding to the data defread(filename): file = open(filename) str_in = file.read() file.close() data = json.loads(str_in) return data COMPSCI 107 - Computer Science Fundamentals
Writing JSON using pretty printing • json.dumps( data ) • json.dumps( data, indent=4, sort_keys=True ) • Formats the output over multiple lines {'b': ['HELLO', 'WORLD'], 'a': ['hello', 'world']} { "a": [ "hello", "world" ], "b": [ "HELLO", "WORLD" ] } COMPSCI 107 - Computer Science Fundamentals
What about user-defined classes? • Point class • Can create a dictionary to store state information then use json • Can use json to read dictionary and extract the state information class Point: def __init__(self, loc_x, loc_y): self.x = loc_x self.y = loc_y defgenerate_json(p): out = {'_Point' : True, 'x' : p.x, 'y' : p.y} returnjson.dumps(out, sorted_keys=True) defgenerate_point(txt): inp = json.loads(txt) result = Point( inp['x'], inp['y'] ) returnresult COMPSCI 107 - Computer Science Fundamentals
Summary • JSON is a standard way to exchange data • Easily parsed by machines • Human readable form • JSON uses dictionaries and lists • Dictionaries are unordered • Lists are ordered • Symbols used in JSON are the same as Python • Double quotes used for strings COMPSCI 107 - Computer Science Fundamentals