Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
996 views
in Technique[技术] by (71.8m points)

java - How to prevent JSON from removing spaces?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...