I need to parse this json file, but it's in such a weird format. https://rsbuddy.com/exchange/summary.json I am using Gson, and I have never dealt with Json before so I am quite lost.
"2": {"buy_average": 191, "id": 2, "sell_average": 191, "name": "Cannonball", "overall_average": 191}
After reading this answer Parsing JSON from URL
I have come up with the code here:
public class AlchemyCalculator {
public static void main(String[] args) throws Exception {
String json = readUrl("https://rsbuddy.com/exchange/summary.json");
Gson gson = new Gson();
Page page = gson.fromJson(json, Page.class);
System.out.println(page.items.size());
for (Item item : page.items)
System.out.println(" " + item.buy_average);
}
private static String readUrl(String urlString) throws Exception {
BufferedReader reader = null;
try {
URL url = new URL(urlString);
reader = new BufferedReader(new InputStreamReader(url.openStream()));
StringBuffer buffer = new StringBuffer();
int read;
char[] chars = new char[1024];
while ((read = reader.read(chars)) != -1)
buffer.append(chars, 0, read);
System.out.println("{items: [" +buffer.substring(1, buffer.length() -1) + "]}");
return "{items: [" +buffer.substring(1, buffer.length() -1) + "]}";
} finally {
if (reader != null)
reader.close();
}
}
static class Item {
int buy_average;
int id;
int sell_average;
String name;
int overall_average;
}
static class Page {
List<Item> items;
}
}
I don't understand how to parse it though, I have seen answers that say to set up a matching object heirarchy, but I don't see how to do that here.
Thanks in advance, as well as apologizing in advance for the millions of Json questions on here and me not being able to understand from those.