Skip to content

Commit

Permalink
Move JSON->Room parsing into Room class.
Browse files Browse the repository at this point in the history
  • Loading branch information
owainkenwayucl committed Nov 26, 2017
1 parent 3625b32 commit 7149683
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 33 deletions.
40 changes: 8 additions & 32 deletions examples/json.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// UCL API example in Java
// This example queries the roombookings/rooms endpoint and converts
// the first response from JSON into a uclapi.Room object.
// This example queries the roombookings/rooms endpoint and prints results.
//
// Owain Kenway

Expand All @@ -11,45 +10,22 @@

import java.util.Hashtable;

import org.json.simple.JSONObject;
import org.json.simple.JSONArray;
import org.json.simple.parser.ParseException;
import org.json.simple.parser.JSONParser;

public class json {
public static void main(String[] args) {
if (args.length == 1) {
if (args.length == 2) {

// Get our response out of the UCL API
UCLApiConnection conn = new UCLApiConnection(args[0]);
Hashtable<String, String> params = new Hashtable<String, String>();
params.put("date", "20171125");
params.put("results_per_page", "1");
String response = conn.queryAPI(UCLApiConnection.RoomRoomsEP, params);

// Convert it into Room objects and print out.
try {
JSONParser p = new JSONParser();
JSONObject responseObject = (JSONObject)p.parse(response);

JSONArray rooms = (JSONArray)responseObject.get("rooms");
int nRooms = rooms.size();
System.out.println("Got " + Integer.toString(nRooms) + " rooms.");
params.put("date", args[1]);

for (int i = 0; i < nRooms; i++) {
JSONObject jroom = (JSONObject)rooms.get(i);
Room[] rooms = Room.searchAPI(conn, params);

Room room = new Room(jroom);
System.out.println(room.toString());
}

} catch (Exception e){
System.err.println(e.toString());
System.exit(5);
for (int i = 0; i<rooms.length; i++) {
System.out.println(rooms[i].toString());
}

} else {
System.err.println("Run with API key as argument.");
System.err.println("Run with API key + date [YYYYMMDD] as arguments.");
System.exit(1);
}
}
Expand Down
39 changes: 38 additions & 1 deletion uclapi/Room.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@
*/
package uclapi;

import uclapi.UCLApiConnection;

import java.lang.Math;
import java.util.Hashtable;

import org.json.simple.JSONObject;
import org.json.simple.JSONArray;
import org.json.simple.parser.ParseException;
import org.json.simple.parser.JSONParser;

public class Room {
public class Room {

// Extracted from JSON example.
private String roomname;
Expand Down Expand Up @@ -75,6 +80,38 @@ public Room(JSONObject jsonroom) {

}

/**
* Perform a query on a given UCL API connection and return an array of rooms.
* @param conn UCLApiConnection
* @param params hashtable of query parameters
* @return Array of Rooms
*/
public static Room[] searchAPI(UCLApiConnection conn, Hashtable<String, String> params) {
String response = conn.queryAPI(UCLApiConnection.RoomRoomsEP, params);

try {
JSONParser p = new JSONParser();
JSONObject responseObject = (JSONObject)p.parse(response);

JSONArray rooms = (JSONArray)responseObject.get("rooms");
int nRooms = rooms.size();
Room[] retval = new Room[nRooms];

for (int i = 0; i < nRooms; i++) {
JSONObject jroom = (JSONObject)rooms.get(i);

retval[i] = new Room(jroom);

}

return retval;

} catch (Exception e){
System.err.println(e.toString());
System.exit(5);
}
return new Room[0];
}
/**
* @return a string representation of the room.
*/
Expand Down

0 comments on commit 7149683

Please sign in to comment.