trainux Posted July 7, 2018 Share Posted July 7, 2018 I have the link "GE" where I get, by Json, the price per ID, of each item, how can I get only the price that said Json contains? Quote Link to comment Share on other sites More sharing options...
Beenrange Posted July 7, 2018 Share Posted July 7, 2018 (edited) Instead of using rs GE, use rsbuddy GE as its more reflective of actual prices imo... Following code was found elsewhere and adopted for my usecase (sorry cant remember source) private final static String RSBUDDY_URL = "https://rsbuddy.com/exchange/summary.json"; public static HashMap<String, Integer> getPriceMap(List<String> items) { HashMap<String, Integer> priceMap = new HashMap<>(); try { URL url = new URL(RSBUDDY_URL); BufferedReader jsonFile = new BufferedReader(new InputStreamReader(url.openStream())); JsonObject priceJSON = JsonObject.readFrom(jsonFile.readLine()); Iterator<Member> iterator = priceJSON.iterator(); while (iterator.hasNext()) { JsonObject itemJSON = priceJSON.get(iterator.next().getName()).asObject(); String itemName = itemJSON.get("name").asString(); if (items.contains(itemName)) { priceMap.put(itemName, itemJSON.get("buy_average").asInt()); } } } catch (Exception e) { } return priceMap; } Then, every time you loot an item or if you already know the item name, add it to a list of strings. You can use it as such.... HashMap<String,Integer> itemPrices = new HashMap(); itemPrices = getPriceMap(items); For keeping a list of total looted you could (inefficiently, sorry unwilling to share my code) for(String item : items){ itemPrices = getPriceMap(items); try { total = total + itemPrices.get(item); } catch (Exception e) { log("Empty"); } } items.clear(); } You can use the following library (literally just copy and paste the .java files into your code, I don't know of a better way of doing this) https://github.com/ralfstx/minimal-json Edited July 7, 2018 by Beenrange 1 Quote Link to comment Share on other sites More sharing options...
trainux Posted July 8, 2018 Author Share Posted July 8, 2018 (edited) I needed something simpler, something to just indicate the ID, thanks for the code fragment and for the advice. Price.java package scripts; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; import java.util.Iterator; import java.util.List; import json.JsonObject; import json.JsonObject.Member; public class Price { private final static String RSBUDDY_URL = "https://rsbuddy.com/exchange/summary.json"; public int getPrice(List<Integer> items) { int price = 0; try { URL url = new URL(RSBUDDY_URL); BufferedReader jsonFile = new BufferedReader(new InputStreamReader(url.openStream())); JsonObject priceJSON = JsonObject.readFrom(jsonFile.readLine()); Iterator<Member> iterator = priceJSON.iterator(); while (iterator.hasNext()) { JsonObject itemJSON = priceJSON.get(iterator.next().getName()).asObject(); int itemID = itemJSON.get("id").asInt(); if (items.contains(itemID)) { price = itemJSON.get("buy_average").asInt(); break; } } } catch (Exception e) { } return price; } } Usage: final Price price = new Price(); ArrayList<Integer> items = new ArrayList<Integer>(); items.add(434); price.getPrice(items); Edited July 8, 2018 by trainux Quote Link to comment Share on other sites More sharing options...
trainux Posted July 8, 2018 Author Share Posted July 8, 2018 23 hours ago, Beenrange said: Instead of using rs GE, use rsbuddy GE as its more reflective of actual prices imo... Following code was found elsewhere and adopted for my usecase (sorry cant remember source) private final static String RSBUDDY_URL = "https://rsbuddy.com/exchange/summary.json"; public static HashMap<String, Integer> getPriceMap(List<String> items) { HashMap<String, Integer> priceMap = new HashMap<>(); try { URL url = new URL(RSBUDDY_URL); BufferedReader jsonFile = new BufferedReader(new InputStreamReader(url.openStream())); JsonObject priceJSON = JsonObject.readFrom(jsonFile.readLine()); Iterator<Member> iterator = priceJSON.iterator(); while (iterator.hasNext()) { JsonObject itemJSON = priceJSON.get(iterator.next().getName()).asObject(); String itemName = itemJSON.get("name").asString(); if (items.contains(itemName)) { priceMap.put(itemName, itemJSON.get("buy_average").asInt()); } } } catch (Exception e) { } return priceMap; } Then, every time you loot an item or if you already know the item name, add it to a list of strings. You can use it as such.... HashMap<String,Integer> itemPrices = new HashMap(); itemPrices = getPriceMap(items); For keeping a list of total looted you could (inefficiently, sorry unwilling to share my code) for(String item : items){ itemPrices = getPriceMap(items); try { total = total + itemPrices.get(item); } catch (Exception e) { log("Empty"); } } items.clear(); } You can use the following library (literally just copy and paste the .java files into your code, I don't know of a better way of doing this) https://github.com/ralfstx/minimal-json thank you very much brother. 1 Quote Link to comment Share on other sites More sharing options...