Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

java - Json decode and again json encode not return same value

I have a JSON string (Created through JAVA). I can decode(PHP : json_decode) and create(PHP:json_encode) again JSON string but the return cant the same as an original JSON string. hash256 value not return same value.

$Requestbody = "{"billerid": "RELENG","short_name": "HOME91", 
"authenticators": [{"parameter_name": "Consumer number","value": 
"155555105"}],"customer": {"customer_name": "OG 
Test","customer_mobile": "1231231234","customer_email": 
"[email protected]","customer_pan": "AEPM123RC4","customer_aadhaar": 
"123123123123"},"metadata": {"agent": {"agentid": 
"BD001MPY100000100001","sub_agentid": "BD001MPY11"},"device": 
{"mobile": "9800000000","geocode": "19.075984,72.877656","postal_code": 
"400053","ip": "124.124.1.1","channel": "INT","branchCode": 
"UTI00030","terminalid": "123123","imei": "123123123","mac": "11-AC-
58-21-1B-AA","os": "iOS","app": "AGENTAPP","user_agent": 
"Mozilla","dvc_fngrprnt": "AB22331DF49A"}}}";
question from:https://stackoverflow.com/questions/65880038/json-decode-and-again-json-encode-not-return-same-value

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

1 Answer

0 votes
by (71.8m points)

Your JSON string contains spaces after a few colons. If these are removed, the strings after json_decode and json_encode are even the same.

$Requestbody = "{"billerid": "RELENG","short_name": "HOME91","authenticators": [{"parameter_name": "Consumer number","value":"155555105"}],"customer": {"customer_name": "OGTest","customer_mobile": "1231231234","customer_email":"[email protected]","customer_pan": "AEPM123RC4","customer_aadhaar":"123123123123"},"metadata": {"agent": {"agentid":"BD001MPY100000100001","sub_agentid": "BD001MPY11"},"device":{"mobile": "9800000000","geocode": "19.075984,72.877656","postal_code":"400053","ip": "124.124.1.1","channel": "INT","branchCode":"UTI00030","terminalid": "123123","imei": "123123123","mac": "11-AC-58-21-1B-AA","os": "iOS","app": "AGENTAPP","user_agent":"Mozilla","dvc_fngrprnt": "AB22331DF49A"}}}";

$arr = json_decode($Requestbody, true);
$str = json_encode($arr);

$cleanStr = str_replace(': ',':',$Requestbody);
var_dump($cleanStr === $str);//bool(true)

However, how @sabik correctly writes in the comment, Json strings cannot normally be compared.

In contrast to this, arrays created with json_decode ($str, true) can be compared directly.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...