• shell实现数据json格式转换


    redis中按hash格式存放的数据,转换到json格式

    例子1:

    redis内数据格式
    test1
    "dbname=test1|username=db2inst3|passwd=db2inst3|ip=192.168.1.61|port=60004|" bmw "dbname=bmw|username=db2inst3|passwd=db2inst3|ip=192.168.1.113|port=50000|" test2 "dbname=test2|username=db2inst3|passwd=db2inst3|ip=192.168.1.113|port=50000|"
    shell:
    awk 'BEGIN{printf "["}' echo "hgetall SrcLogInfo " | ../redis | while read line do strA="|" #num=echo ${line | wc -c} awk 'BEGIN{printf "{"}' ret="$line" echo ${ret} result=$(echo $line | grep "${strA}") if [ -n "$result" ]; then awk 'BEGIN{printf "},"}' else awk 'BEGIN{printf ":"}' fi done awk 'BEGIN{printf "]"}'
    转换结果为:json格式
    [{
            "test1": "dbname=test1|username=db2inst3|passwd=db2inst3|ip=192.168.1.61|port=60004|"
        },
        {
            "bmw": "dbname=bmw|username=db2inst3|passwd=db2inst3|ip=192.168.1.113|port=50000|"
        },
        {
            "test2": "dbname=test2|username=db2inst3|passwd=db2inst3|ip=192.168.1.113|port=50000|"
        }
    ]

    例子2:

    redis存的数据格式:
    1) "db2inst1"
    2) "{"serverip":"192.168.1.113","dbip":"192.168.1.225","dbport":"50000","nodename":"node225","dbname":"db2_10","dbaliname":"db2inst1","instname":"db2inst1"}"
    3) "fea"
    4) "{"serverip":"192.168.1.113","dbip":"192.168.1.225","dbport":"50000","nodename":"node225","dbname":"db2_10","dbaliname":"fea","instname":"db2inst1"}"
    5) "bmw"
    6) "{"serverip":"192.168.1.113","dbip":"192.168.1.61","dbport":"60004","nodename":"node61","dbname":"test1","dbaliname":"bmw","instname":"db2inst3"}"
    shell:
    awk 'BEGIN{printf "["}' echo "hgetall catainfo " | ../redis| while read line do i=$(($i+1)) if [[ $(($i%2)) -eq 0 ]];then printf $line, fi done awk 'BEGIN{printf "]"}'

    //最后格式就不展示了
  • 相关阅读:
    360手机青春版原装充电器
    然并卵却很拉风!360超级充电器评测_天极网
    39元超值!360超级充电器拆解与评测
    99元紫米10000mAh移动电源开箱拆解
    自己DIY PCB电路板制作(简单方便快捷)
    Easy EDA:在线创客软件服务是未来的趋势 | 雷锋网
    EasyEDA
    我的那些年(9)~我来团队了,Mvc兴起了
    我的那些年(8)~去朋友公司做网站开发
    我的那些年(7)~第一份互联网工作
  • 原文地址:https://www.cnblogs.com/kony9527/p/10534292.html
Copyright © 2020-2023  润新知