我已經用Java編碼了幾個月了,但是在網絡編程方面我幾乎沒有任何經驗,所以如果我的問題很愚蠢,請原諒我。現在,我想,它看起來更好的方式來格式化這個JSON數據: https://apps.runescape.com/runemetrics/profile/profile?user=Gragoyle&activities=20在Java中格式化JSON數據
伊夫擡頭多個解決方案,都顯得相當混亂,不正是我想要它。如果你能解釋我如何格式化這些原始文本,那將是非常好的。
我已經用Java編碼了幾個月了,但是在網絡編程方面我幾乎沒有任何經驗,所以如果我的問題很愚蠢,請原諒我。現在,我想,它看起來更好的方式來格式化這個JSON數據: https://apps.runescape.com/runemetrics/profile/profile?user=Gragoyle&activities=20在Java中格式化JSON數據
伊夫擡頭多個解決方案,都顯得相當混亂,不正是我想要它。如果你能解釋我如何格式化這些原始文本,那將是非常好的。
GSON是一個Java序列化/反序列化庫,可以將Java對象轉換爲JSON並返回。
使用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);
}
輸出:
欲瞭解更多信息,請this
下載GSON,檢查this
從Java的角度來說,它是這樣的:
也許您會發現我的答案爲「generic」;但是,你的意見也是如此。你不是在說你正在掙扎着什麼,因此我只能通過給你提供從JSON原始數據到格式化文本的配方。
使用GSON下面的樣品的方法:
/**
* Convert a JSON string to pretty print version
* @param jsonString
* @return
*/
public static String toPrettyFormat(String jsonString)
{
JsonParser parser = new JsonParser();
JsonObject json = parser.parse(jsonString).getAsJsonObject();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String prettyJson = gson.toJson(json);
return prettyJson;
}
謝謝,這個工作:) –
感謝這麼多,工作完全罰款 –
歡迎您,很高興幫助:) –