Upload
ashlie-harrison
View
218
Download
1
Embed Size (px)
Citation preview
JSON
Android Club 2015
Agenda
• JSON• Parsing• ListView
What is JSON?
• JavaScript Object Notation• Minimal• Readable format• Server <-> Client• Alternative to XML• Language independant
JSON object: example
{ "id": 9999, "firstName": "Joe", "lastName": "Richard“
}
JSON object: practice
Create object for one book:author: Haruki Murakamititle: Norwegian Woodyear: 1987
You can check JSON inwww.jsoneditoronline.org
JSON array: example
{ "employees": [ { "firstName": "Joe", "lastName": "Richard" }, { "firstName": "Anna", "lastName": "Kim" }, { "firstName": "Kain", "lastName": "Saridzawa" } ]}
JSON array: practice
• Create array: students• Add three students:• Each student should contain• 1) id• 2) first_name• 3) last_name
JSON symbols
• { object }• [ array ]• : equals• “variable/object name”• “String”• int
JSON parsing: example
• http://joerichard.net/api/ac/student.json
• JSONObject object = new JSONObject(s);String id = object.getString("id");MainActivity.tvData.setText("My id is "+id);
JSON parsing: practice
• http://jsonip.com• Parse JSON object• Take its ip• Message: “My ip is 192.168.1.1”
JSON object parsing: example
• http://joerichard.net/api/ac/student.json
• Create Student POJO• It should include: id, first_name,
last_name• Create JSON object to Student object
JSON object parsing: practice
• http://jsonip.com• Create POJO: JsonIP• It should have: ip, about, Pro!• Convert JSON Object to JsonIP object
JSON array: example
http://joerichard.net/api/ac/employees.jsonJSONObject object = new JSONObject(s);JSONArray employees = object.getJSONArray("employees");StringBuilder sb = new StringBuilder();for (int i = 0; i < employees.length(); i++) { JSONObject employee = employees.getJSONObject(i); String firstName = employee.getString("firstName"); String lastName = employee.getString("lastName"); sb.append(firstName+" "+lastName+"\n");}MainActivity.tvData.setText(sb.toString());
JSON array: practice
• http://joerichard.net/api/ac/tariffs.json• Show list of tarrifs
JSON array ->Object list: example
• http://joerichard.net/api/ac/employees.json
• Create POJO: Employee• It should include: firstName, lastName• Convert JSON array to Object list• Show amount of list items
JSON array -> POJO list: practice
• http://joerichard.net/api/ac/tariffs.json• Create POJO: Tariff• It should contain: id, name• Convert JSON array to Tariff list• Show size of list
Homework
• http://joerichard.net/api/tw/tw.json• Take data from Object->daily->data
array• Show weather data in TextView in this
form• 22-07-2015 21c-34c• 23-07-2015 25c-40c• … like this for 7 days
Questions?
• Any questions?
Thank you
• Thank you for your attention!