1 / 10

COMPSCI 107 Computer Science Fundamentals

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

stamos
Télécharger la présentation

COMPSCI 107 Computer Science Fundamentals

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMPSCI 107Computer Science Fundamentals Lecture 09 – JSON

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

More Related