Simon Hostettler Simon Hostettler - 3 months ago 8
Java Question

Formatting JSON data in Java

Ive been coding with Java for a few months now, but i have almost zero experience when it comes to web-programming, so excuse me if my question is stupid. Now I wanted to format this JSON data in a way that it looks better :
https://apps.runescape.com/runemetrics/profile/profile?user=Gragoyle&activities=20

like this : http://imgur.com/PZffFDc

Ive looked up multiple solutions which all seem pretty confusing and not exactly how I wanted it. It would be very nice if you could explain me how I can format this raw text.

Answer

GSON is a Java serialization/deserialization library that can convert Java Objects into JSON and back.

Try this code using GSON:

public static void main(String[] args) {
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        JsonParser jp = new JsonParser();
        String uglyJSONString = "{\"magic\":7322944,\"questsstarted\":8,\"totalskill\":2051,\"questscomplete\":109,\"questsnotstarted\":107,\"totalxp\":101280135,\"ranged\":14577633,\"activities\":[{\"date\":\"03-Sep-2016 19:45\",\"details\":\"Now that the Piscatoris fishing colony is safe, the Wise Old Man has returned to Draynor to retire from his adventurous life.\",\"text\":\"Quest complete: Swan Song\"},{\"date\":\"31-Aug-2016 21:00\",\"details\":\"I levelled my  Fishing skill, I am now level 66.\",\"text\":\"Levelled up Fishing.\"},{\"date\":\"29-Aug-2016 20:56\",\"details\":\"I levelled my  Fishing skill, I am now level 65.\",\"text\":\"Levelled up Fishing.\"},{\"date\":\"28-Aug-2016 21:17\",\"details\":\"I levelled my  Woodcutting skill, I am now level 77.\",\"text\":\"Levelled up Woodcutting.\"},{\"date\":\"28-Aug-2016 00:27\",\"details\":\"I levelled my  Attack skill, I am now level 96.\",\"text\":\"Levelled up Attack.\"},{\"date\":\"28-Aug-2016 00:13\",\"details\":\"After killing a Dark beast, it dropped a dragon helm.\",\"text\":\"I found a dragon helm\"},{\"date\":\"28-Aug-2016 00:13\",\"details\":\"I levelled my  Slayer skill, I am now level 95.\",\"text\":\"Levelled up Slayer.\"},{\"date\":\"27-Aug-2016 20:50\",\"details\":\"After killing a Gargoyle, it dropped a granite maul.\",\"text\":\"I found a granite maul\"},{\"date\":\"27-Aug-2016 20:46\",\"details\":\"After killing a Gargoyle, it dropped a granite maul.\",\"text\":\"I found a granite maul\"},{\"date\":\"27-Aug-2016 20:17\",\"details\":\"I levelled my  Attack skill, I am now level 95.\",\"text\":\"Levelled up Attack.\"},{\"date\":\"27-Aug-2016 18:51\",\"details\":\"I now have at least 14000000 experience points in the Constitution skill.\",\"text\":\"14000000XP in Constitution\"},{\"date\":\"27-Aug-2016 13:41\",\"details\":\"After killing a Mutated jadinko male, it dropped a whip vine.\",\"text\":\"I found a whip vine\"},{\"date\":\"27-Aug-2016 13:35\",\"details\":\"After killing a Mutated jadinko male, it dropped a whip vine.\",\"text\":\"I found a whip vine\"},{\"date\":\"26-Aug-2016 23:35\",\"details\":\"I levelled my  Slayer skill, I am now level 94.\",\"text\":\"Levelled up Slayer.\"},{\"date\":\"26-Aug-2016 22:47\",\"details\":\"After killing a Dark beast, it dropped a dark bow.\",\"text\":\"I found a dark bow\"},{\"date\":\"26-Aug-2016 22:47\",\"details\":\"I levelled my  Attack skill, I am now level 94.\",\"text\":\"Levelled up Attack.\"},{\"date\":\"26-Aug-2016 22:35\",\"details\":\"I levelled my  Thieving skill, I am now level 67.\",\"text\":\"Levelled up Thieving.\"},{\"date\":\"26-Aug-2016 21:08\",\"details\":\"I levelled my  Firemaking skill, I am now level 76.\",\"text\":\"Levelled up Firemaking.\"},{\"date\":\"25-Aug-2016 16:34\",\"details\":\"I levelled my  Attack skill, I am now level 93.\",\"text\":\"Levelled up Attack.\"},{\"date\":\"24-Aug-2016 20:41\",\"details\":\"After killing a Ice strykewyrm, it dropped a staff of light.\",\"text\":\"I found a staff of light\"}],\"skillvalues\":[{\"level\":99,\"xp\":175680443,\"rank\":88376,\"id\":4},{\"level\":99,\"xp\":145776331,\"rank\":190066,\"id\":3},{\"level\":99,\"xp\":139648694,\"rank\":134410,\"id\":1},{\"level\":96,\"xp\":100774564,\"rank\":200142,\"id\":0},{\"level\":95,\"xp\":90523125,\"rank\":133284,\"id\":18},{\"level\":93,\"xp\":73229446,\"rank\":198748,\"id\":5},{\"level\":89,\"xp\":50699126,\"rank\":157375,\"id\":23},{\"level\":81,\"xp\":23157603,\"rank\":414400,\"id\":6},{\"level\":81,\"xp\":22893105,\"rank\":130514,\"id\":25},{\"level\":81,\"xp\":22126995,\"rank\":192969,\"id\":12},{\"level\":79,\"xp\":19769900,\"rank\":487102,\"id\":2},{\"level\":78,\"xp\":17627591,\"rank\":248295,\"id\":24},{\"level\":77,\"xp\":15277305,\"rank\":199776,\"id\":22},{\"level\":77,\"xp\":14769284,\"rank\":484339,\"id\":8},{\"level\":76,\"xp\":14414476,\"rank\":369076,\"id\":11},{\"level\":76,\"xp\":14293211,\"rank\":213478,\"id\":16},{\"level\":75,\"xp\":13146625,\"rank\":265506,\"id\":15},{\"level\":75,\"xp\":12996081,\"rank\":289349,\"id\":14},{\"level\":73,\"xp\":10076779,\"rank\":408471,\"id\":9},{\"level\":72,\"xp\":9915667,\"rank\":301575,\"id\":13},{\"level\":67,\"xp\":5936573,\"rank\":530243,\"id\":7},{\"level\":67,\"xp\":5905270,\"rank\":268005,\"id\":17},{\"level\":66,\"xp\":5454333,\"rank\":506599,\"id\":10},{\"level\":64,\"xp\":4099952,\"rank\":285374,\"id\":19},{\"level\":60,\"xp\":2843575,\"rank\":383683,\"id\":21},{\"level\":55,\"xp\":1765392,\"rank\":439089,\"id\":20},{\"level\":1,\"xp\":0,\"rank\":0,\"id\":26}],\"name\":\"Gragoyle\",\"rank\":\"238,029\",\"melee\":57622502,\"combatlevel\":136,\"loggedIn\":\"false\"}";
        JsonElement je = jp.parse(uglyJSONString);
        String prettyJsonString = gson.toJson(je);
        System.out.println(prettyJsonString);
}

Output:

enter image description here

For more info, check this

To download GSON, check this

Comments