How to prevent JSON from removing spaces?

不羁的心 提交于 2019-12-12 04:39:53

问题


I am using the json.simple library. And I want to stick to that, if possible. When i retrieve an Object

String blockString = message.get("block").toString();

and then

 System.out.println(blockString)

gives me this

{"type": "block", "transactions": [], "timestamp": "2017-11-02T06:17:05.079481", "reward": "b76364406658d679667e5e4be043f2da9d1f0e4ead17d78fcf152a5d718411c06d5dd2922cdb86a25d09c76dde7d26e8e26a55a90211bc6031f5d3b1cedc5b9c0577c01a1af3b8099261aa83fe6751c317662df130c10df56d4891fd8c1281a5d1bcb45d23b3096384b44a4a7af0e5217b7543eb68660914f936cdcdbe7b4c06851cb0efdef4c3859e7d38ac1da965430d498bcf4854b5d0470a421b2ddd50c61eac3f0e1f80f652daa90b5e1314a84e8fbadc7ad9dd1cdea613d7c292efe64f60d6ea2cbea0153701930a1fbf32d5d28a3d10e85adf8d2390dd7d551204f8fb430d8e0816f347269850751f6b00e23c1f125ff4cda7fd3de723e34aaaaa6599", "difficulty": "0", "nonce": "4c715812ebb33b38ec7546463112a3f9601dd05df13e4124f6ca5c491fb5d5883c941810da5dfa1a9902201af1c1d122476e1544a0ade75f7c05644ff25d8f2c", "parent": "00000f6c2e83499bd66b82128d523b4e6e17f289c938447c26f2edb20d382de818e332bf35349f92a5316657b9fd9a1e7b92b7c2aa709d7f487ea696582eec03"}

which is exactly what I want to have. Now, when I do the following:

JSONObject blockPayLoad = (JSONObject) JSONValue.parse(blockString);

and then

System.out.println(blockPayLoad.toString());

I get this

{"reward":"b76364406658d679667e5e4be043f2da9d1f0e4ead17d78fcf152a5d718411c06d5dd2922cdb86a25d09c76dde7d26e8e26a55a90211bc6031f5d3b1cedc5b9c0577c01a1af3b8099261aa83fe6751c317662df130c10df56d4891fd8c1281a5d1bcb45d23b3096384b44a4a7af0e5217b7543eb68660914f936cdcdbe7b4c06851cb0efdef4c3859e7d38ac1da965430d498bcf4854b5d0470a421b2ddd50c61eac3f0e1f80f652daa90b5e1314a84e8fbadc7ad9dd1cdea613d7c292efe64f60d6ea2cbea0153701930a1fbf32d5d28a3d10e85adf8d2390dd7d551204f8fb430d8e0816f347269850751f6b00e23c1f125ff4cda7fd3de723e34aaaaa6599","difficulty":"0","parent":"00000f6c2e83499bd66b82128d523b4e6e17f289c938447c26f2edb20d382de818e332bf35349f92a5316657b9fd9a1e7b92b7c2aa709d7f487ea696582eec03","type":"block","transactions":[],"nonce":"4c715812ebb33b38ec7546463112a3f9601dd05df13e4124f6ca5c491fb5d5883c941810da5dfa1a9902201af1c1d122476e1544a0ade75f7c05644ff25d8f2c","timestamp":"2017-11-02T06:17:05.079481"}

So two concerns/questions

  1. How can I keep the spaces?
  2. How can I keep the previous order (starting with "type"..etc)?

Reason is, I am hashing the entire thing, and therefore I cannot tamper with whitespaces/order of the objects.

来源:https://stackoverflow.com/questions/47404356/how-to-prevent-json-from-removing-spaces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!