我已经用Java编写了几个月的编码,但是我在网络编程方面的经验几乎为零,所以如果我的问题很愚蠢,请原谅。现在我想以一种看起来更好的方式格式化这个JSON数据: https://apps.runescape.com/runemetrics/profile/profile?user=Gragoyle&activities=20
我找到了多个解决方案,这些解决方案看起来都很混乱,而且不是我想要的。如果你能解释我如何格式化这个原始文本,那将是非常好的。
答案 0 :(得分:2)
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
答案 1 :(得分:1)
使用GSON下面的示例方法:
set
答案 2 :(得分:0)
从Java的角度来看,它是这样的:
也许你找到了我对“通用”的回答;但是,你的意见也是如此。你没有说你究竟在挣扎什么;因此,我只能为您提供从JSON原始数据到格式化文本的配方。