支付宝签名和验签使用JSONObject是最优解。json字符串顺序和==符号都一致演示代码
支付宝spi接口设计验签和返回结果加签注意点,支付宝使用JSONObject对象
https://www.cnblogs.com/oktokeep/p/18249346
package com.example.core.mydemo; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.example.core.mydemo.json2.GsonUtils; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import java.util.HashMap; import java.util.Map; public class MapTest { private static final Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create(); // 定义jackson对象 private static final ObjectMapper MAPPER = new ObjectMapper(); public static void main(String[] args) throws JsonProcessingException { /** * output: * {aaa=111, ccc=333, bbb=222} * gson={"aaa":"111","ccc":"333","bbb":"222"} * JSON={"aaa":"111","ccc":"333","bbb":"222"} * JSONObject={"aaa":"111","ccc":"333","bbb":"222"} */ Map<String,String> map = new HashMap<>(); map.put("aaa","111"); map.put("bbb","222"); map.put("ccc","333"); System.out.println(map); System.out.println("gson="+gson.toJson(map)); System.out.println("JSON="+JSON.toJSONString(map)); JSONObject jsonObject = (JSONObject) JSON.toJSON(map); System.out.println("JSONObject="+jsonObject.toJSONString()); /** * output: 结论:gson会转义编码符号, * ==符号错误,不一致 * gson={"user":{"name":"刘德华","age":50,"url":"http://www.baidu.com/\u003d\u003d"},"sign":"签名串"} * gson-userJson={"name":"刘德华","age":50,"url":"http://www.baidu.com/=="} * * json字符串顺序不一致 * JSON={"sign":"签名串","user":{"age":50,"name":"刘德华","url":"http://www.baidu.com/=="}} * JSON-userJson={"name":"刘德华","age":50,"url":"http://www.baidu.com/=="} * * 使用JSONObject是最优解。json字符串顺序和==符号都一致 * JSONObject={"sign":"签名串","user":{"name":"刘德华","age":50,"url":"http://www.baidu.com/=="}} * JSONObject-userJson={"name":"刘德华","age":50,"url":"http://www.baidu.com/=="} * * 使用jackson也可以保持json字段顺序和==符号一致 * MAPPER={"user":{"name":"刘德华","age":50,"url":"http://www.baidu.com/=="},"sign":"签名串"} * MAPPER-userJson={"name":"刘德华","age":50,"url":"http://www.baidu.com/=="} */ User user = new User(); user.setName("刘德华"); user.setAge(50); user.setUrl("http://www.baidu.com/=="); People people = new People(); people.setUser(user); people.setSign("签名串"); String json1 = new Gson().toJson(people); System.out.println("gson=" + json1); JSONObject JSONObject = JSON.parseObject(json1); String userJson = JSONObject.getString("user"); System.out.println("gson-userJson=" + userJson); json1 = JSON.toJSONString(people); System.out.println("JSON=" + json1); JSONObject = JSON.parseObject(json1); userJson = JSONObject.getString("user"); System.out.println("JSON-userJson=" + userJson); JSONObject jsonObject2 = (JSONObject) JSON.toJSON(people); json1 = jsonObject2.toJSONString(); System.out.println("JSONObject=" + json1); JSONObject = JSON.parseObject(json1); userJson = JSONObject.getString("user"); System.out.println("JSONObject-userJson=" + userJson); json1 = MAPPER.writeValueAsString(people); System.out.println("MAPPER=" + json1); JSONObject = JSON.parseObject(json1); userJson = JSONObject.getString("user"); System.out.println("MAPPER-userJson=" + userJson); } static class People{ User user; String sign; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String getSign() { return sign; } public void setSign(String sign) { this.sign = sign; } } static class User{ private String name; private Integer age; private String url; public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } } }
标签:JSONObject,和验,JSON,json,userJson,user,com,age From: https://www.cnblogs.com/oktokeep/p/18249351