local function encodeBase64(source_str)
    local b64chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
    local s64 = ''
    local str = source_str

    while #str > 0 do
        local bytes_num = 0
        local buf = 0

        for byte_cnt=1,3 do
            buf = (buf * 256)
            if #str > 0 then
                buf = buf + string.byte(str, 1, 1)
                str = string.sub(str, 2)
                bytes_num = bytes_num + 1
            end
        end

        for group_cnt=1,(bytes_num+1) do
            local b64char = math.fmod(math.floor(buf/262144), 64) + 1
            s64 = s64 .. string.sub(b64chars, b64char, b64char)
            buf = buf * 64
        end

        for fill_cnt=1,(3-bytes_num) do
            s64 = s64 .. '='
        end
    end

    return s64
end





str = '{"customer_id":"4901","device_no":"o_xct_4i_r1zp4j0_hriuj3p_ia_bdp_rw","product_carts":[{"product_id":1275,"productunit_id":2193,"amount":0},{"product_id":232,"productunit_id":433,"amount":0},{"product_id":588,"productunit_id":1026,"amount":0},{"product_id":1359,"productunit_id":2374,"amount":0},{"product_id":42,"productunit_id":76,"amount":0},{"product_id":714,"productunit_id":1245,"amount":4},{"product_id":231,"productunit_id":429,"amount":2},{"product_id":53,"productunit_id":94,"amount":1}]}';

-- print(str);
str = string.gsub(str,'"',"");
-- str = string.gsub(str,'{',"");
-- str = string.gsub(str,'}',"");
-- str = string.gsub(str,':',"");
-- -- str = string.gsub(str,'[',"");
-- str = string.gsub(str,']',"");
-- print(str);
str = encodeBase64(str);
-- local strTable = loadstring("return "..str)();

--  print(str);

local strTable = {};


i = 0;
while(i<string.len(str))
do
    --  print("i = ",i)
    s1 = string.sub(str,i,i);
    --   print("s1 = ",s1)
    --   print("i = ",i)
     table.insert(strTable, s1)
   i=i+1
end

-- printTable(strTable);
table.sort(strTable);
-- printTable(strTable);

print(table.concat(strTable));

js es6 语法

////**
//// * 实现lua的table排序对json 格式化排序
//// *
//// * */
////
////function ksort(o) {
////    let sorted = {},
////        keys = Object.keys(o);
////    keys.sort();
////    keys.forEach((key) => {
////        sorted[key] = o[key];
////    })
////    return sorted;
////}
////
////
////function mulitKsort(o) {
////
////    if (Object.prototype.toString.call(o) === '[object Object]') {
////        o = ksort(o);
////    }
////    for (const key in o) {
////        if (Object.prototype.toString.call(o[key]) === '[object Object]') {
////            o[key] = mulitKsort(o[key]);
////        }
////    }
////    return o;
////}
////
////var obj = JSON.parse('{"b":{"g":1,"d":"b克拉说的话"},"a":{"z":[1,"拉开手机丢了饭卡就是离开",3,4],"y":5,"x":{"p":2,"o":1}}}');
////
////console.log(mulitKsort(obj));
//
最后修改:2022 年 08 月 23 日
如果觉得我的文章对你有用,请随意赞赏