Jump to content
Frequently Asked Questions
  • Are you not able to open the client? Try following our getting started guide
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS/Crypto gold? You can purchase vouchers from other users
  • Try asking for help in the chatbox
  • Json Parsing


    Astrate
     Share

    Recommended Posts

    So I'm attempting to use a snippet by - alotoffluffy, Its a Ge price grabber, But everytime I try to call

    log("Item price: " + PriceFetcher.getPricedItem(861).getOverallAverage());
    

    It returns nothing... Here's the snippet below

    import com.google.gson.Gson;
    import com.google.gson.JsonObject;
    import com.google.gson.JsonParser;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.URL;
    
    public class PriceFetcher {
        private static final long HALF_HOUR = 1800000;
        private static final String url = "https://rsbuddy.com/exchange/summary.json";
        private static long cacheTime;
        private static String cache = null;
    
        public static PricedItem getPricedItem(int id){
            if (shouldUpdate())
                fetchJSON();
            JsonObject query = new JsonParser().parse(cache).getAsJsonObject().getAsJsonObject(Integer.toString(id));
            Gson gson = new Gson();
            return gson.fromJson(query, PricedItem.class);
    
        }
    
        private static boolean shouldUpdate() {
            if (cache == null)
                return true;
            else if (System.currentTimeMillis() - cacheTime > HALF_HOUR)
                return true;
            return false;
        }
    
        private static void fetchJSON() {
            try (BufferedReader rd = new BufferedReader(new InputStreamReader(new URL(url).openConnection().getInputStream()))) {
                StringBuilder totalString = new StringBuilder();
                String line;
                while( (line = rd.readLine()) != null) {
                    totalString.append(line);
                }
                cacheTime = System.currentTimeMillis();
                cache = totalString.toString();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public class PricedItem {
            private int id, sp, buy_average, buy_quantity, sell_average, sell_quantity, overall_average, overall_quantity;
            private String name;
            private boolean members;
    
            public PricedItem() {}
    
            public int getShopPrice() {
                return sp;
            }
    
            public void setShopPrice(int sp) {
                this.sp = sp;
            }
    
            public int getId() {
                return id;
            }
    
            public void setId(int id) {
                this.id = id;
            }
    
            public int getBuyAverage() {
                return buy_average;
            }
    
            public void setBuyAverage(int buyAverage) {
                this.buy_average = buyAverage;
            }
    
            public int getBuyQuantity() {
                return buy_quantity;
            }
    
            public void setBuyQuantity(int buy_quantity) {
                this.buy_quantity = buy_quantity;
            }
    
            public int getSellAverage() {
                return sell_average;
            }
    
            public void setSellAverage(int sellAverage) {
                this.sell_average = sellAverage;
            }
    
            public int getSellQuantity() {
                return sell_quantity;
            }
    
            public void setSellQuantity(int sellQuantity) {
                this.sell_quantity = sellQuantity;
            }
    
            public int getOverallAverage() {
                return overall_average;
            }
    
            public void setOverallAverage(int overallAverage) {
                this.overall_average = overallAverage;
            }
    
            public int getOverallQuantity() {
                return overall_quantity;
            }
    
            public void setOverallQuantity(int overallQuantity) {
                this.overall_quantity = overallQuantity;
            }
    
            public String getName() {
                return name;
            }
    
            public void setName(String name) {
                this.name = name;
            }
    
            public boolean isMembers() {
                return members;
            }
    
            public void setMembers(boolean members) {
                this.members = members;
            }
        }
    
    }

    I've never done Json parsing so this is a learning curve, I've looked up multiple tutorials, Can anyone tell me what could make it not return anything in console?

    Edited by Astrate
    Link to comment
    Share on other sites

    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now
     Share

    ×
    ×
    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.