xOx Posted October 30, 2016 Posted October 30, 2016 import static BotHoe.GrandExchangeApi.*; public class Test { public static void main(String[] args) { GrandExchangeApi exchangeApi = new GrandExchangeApi(); GELookupResult lookupResult = exchangeApi.lookup(333); if(lookupResult != null) System.out.println("Item: " + lookupResult.name + " Price: " + lookupResult.price); } } if GrandExchangeApi exchangeApi = new GrandExchangeApi(false); private static GELookupResult parse(int itemId, String json) { Pattern pattern = Pattern.compile("\"(?<key>[^\"]+)\":\"(?<value>[^\"]+)\""); Matcher m = pattern.matcher(json); Map<String, String> results = new HashMap<>(); while(m.find()) { results.put(m.group("key"), m.group("value")); } int price = 0; Matcher priceMatcher = Pattern.compile("\"price\":(?<price>\\d+)").matcher(json); if (priceMatcher.find()) { price = Integer.parseInt(priceMatcher.group("price")); } Can someone explain?
Botre Posted October 30, 2016 Posted October 30, 2016 import static BotHoe.GrandExchangeApi.*; public class Test { public static void main(String[] args) { GrandExchangeApi exchangeApi = new GrandExchangeApi(); GELookupResult lookupResult = exchangeApi.lookup(333); if(lookupResult != null) System.out.println("Item: " + lookupResult.name + " Price: " + lookupResult.price); } } if GrandExchangeApi exchangeApi = new GrandExchangeApi(false); private static GELookupResult parse(int itemId, String json) { Pattern pattern = Pattern.compile("\"(?<key>[^\"]+)\":\"(?<value>[^\"]+)\""); Matcher m = pattern.matcher(json); Map<String, String> results = new HashMap<>(); while(m.find()) { results.put(m.group("key"), m.group("value")); } int price = 0; Matcher priceMatcher = Pattern.compile("\"price\":(?<price>\\d+)").matcher(json); if (priceMatcher.find()) { price = Integer.parseInt(priceMatcher.group("price")); } Can someone explain? 1. Snippet that searches GE data for an object with id 333 and then prints name + price. 2. Part of a JSON parsing method, also for GE data retrieving. 1
xOx Posted October 30, 2016 Author Posted October 30, 2016 1. Snippet that searches GE data for an object with id 333 and then prints name + price. 2. Part of a JSON parsing method, also for GE data retrieving. Thanks. This? public class GrandExchangeApi { private static final String API_LOCATION = "http://services.runescape.com/m=itemdb_oldschool" + "/api/catalogue/detail.json?item=%d"; private static final long TEN_MINUTES = 600000; private final Map<Integer, GELookupResult> cache; private long startTime;
Botre Posted October 30, 2016 Posted October 30, 2016 Thanks. This? public class GrandExchangeApi { private static final String API_LOCATION = "http://services.runescape.com/m=itemdb_oldschool" + "/api/catalogue/detail.json?item=%d"; private static final long TEN_MINUTES = 600000; private final Map<Integer, GELookupResult> cache; private long startTime; Ehm... Variables. API_LOCATION -> where the JSON data is retrieved from. TEN_MINUTES -> The amount (10 * 60 * 1000 = 600000) of milliseconds contained in 10 minutes. cache -> A cache, map mapping object ID to GE data. startTime -> System time when the object was initialized. I'm just guessing from the context, I didn't write this :p 1
Krys Posted October 30, 2016 Posted October 30, 2016 Thanks. @@close @@Decode if you want to close your thread just report it the next time