package com.cy.xpaycore;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cy.xpaycore.base.Util;
import org.junit.Test;
import sun.security.provider.MD5;

import java.net.URLEncoder;
import java.util.*;

public class Demo {
    @Test
    public void run1() {
        String gs="[{\"name\":\"apple\",\"price\":\"100\"},{\"name\":\"banana\",\"price\":\"20\"}]";
        Map<String, Object> map = new HashMap<String, Object>() {{
            put("outTradeNo", "123");
            put("authCode", "1232132");
            put("totalAmount", "100");
            put("goodsDetail",JSON.parseArray(gs));
        }};
        //对Map进行ASCII升序排序
        Set<Map.Entry<String, Object>> set=map.entrySet();
        List<Map.Entry<String,Object>> list=new ArrayList<Map.Entry<String, Object>>(set);
        Collections.sort(list, new Comparator<Map.Entry<String, Object>>() {
            @Override
            public int compare(Map.Entry<String, Object> o1, Map.Entry<String, Object> o2) {
                return o1.getKey().compareTo(o2.getKey());
            }
        });
        StringBuilder str=new StringBuilder();
        for(Map.Entry<String,Object> m:list){
            str.append(String.format("%s=%s&",m.getKey(),m.getValue()));
        }
        String str2=str.substring(0,str.length()-1);
        System.out.println("排序前:"+JSON.toJSONString(map));
        System.out.println(String.format("待签名字符串:%s",str2));

    }


}
输出:
{"totalAmount":"100","authCode":"1232132","outTradeNo":"123","goodsDetail":[{"price":"100","name":"apple"},{"price":"20","name":"banana"}]}
待签名字符串:authCode=1232132&goodsDetail=[{"price":"100","name":"apple"},{"price":"20","name":"banana"}]&outTradeNo=123&totalAmount=100

Process finished with exit code 0