19
JSON Android Club 2015

JSON Android Club 2015. Agenda JSON Parsing ListView

Embed Size (px)

Citation preview

Page 1: JSON Android Club 2015. Agenda JSON Parsing ListView

JSON

Android Club 2015

Page 2: JSON Android Club 2015. Agenda JSON Parsing ListView

Agenda

• JSON• Parsing• ListView

Page 3: JSON Android Club 2015. Agenda JSON Parsing ListView

What is JSON?

• JavaScript Object Notation• Minimal• Readable format• Server <-> Client• Alternative to XML• Language independant

Page 4: JSON Android Club 2015. Agenda JSON Parsing ListView

JSON object: example

{ "id": 9999, "firstName": "Joe", "lastName": "Richard“

}

Page 5: JSON Android Club 2015. Agenda JSON Parsing ListView

JSON object: practice

Create object for one book:author: Haruki Murakamititle: Norwegian Woodyear: 1987

You can check JSON inwww.jsoneditoronline.org

Page 6: JSON Android Club 2015. Agenda JSON Parsing ListView

JSON array: example

{ "employees": [ { "firstName": "Joe", "lastName": "Richard" }, { "firstName": "Anna", "lastName": "Kim" }, { "firstName": "Kain", "lastName": "Saridzawa" } ]}

Page 7: JSON Android Club 2015. Agenda JSON Parsing ListView

JSON array: practice

• Create array: students• Add three students:• Each student should contain• 1) id• 2) first_name• 3) last_name

Page 8: JSON Android Club 2015. Agenda JSON Parsing ListView

JSON symbols

• { object }• [ array ]• : equals• “variable/object name”• “String”• int

Page 9: JSON Android Club 2015. Agenda JSON Parsing ListView

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);

Page 10: JSON Android Club 2015. Agenda JSON Parsing ListView

JSON parsing: practice

• http://jsonip.com• Parse JSON object• Take its ip• Message: “My ip is 192.168.1.1”

Page 11: JSON Android Club 2015. Agenda JSON Parsing ListView

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

Page 12: JSON Android Club 2015. Agenda JSON Parsing ListView

JSON object parsing: practice

• http://jsonip.com• Create POJO: JsonIP• It should have: ip, about, Pro!• Convert JSON Object to JsonIP object

Page 13: JSON Android Club 2015. Agenda JSON Parsing ListView

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());

Page 14: JSON Android Club 2015. Agenda JSON Parsing ListView

JSON array: practice

• http://joerichard.net/api/ac/tariffs.json• Show list of tarrifs

Page 15: JSON Android Club 2015. Agenda JSON Parsing ListView

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

Page 16: JSON Android Club 2015. Agenda JSON Parsing ListView

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

Page 17: JSON Android Club 2015. Agenda JSON Parsing ListView

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

Page 18: JSON Android Club 2015. Agenda JSON Parsing ListView

Questions?

• Any questions?

Page 19: JSON Android Club 2015. Agenda JSON Parsing ListView

Thank you

• Thank you for your attention!