Home  >  Q&A  >  body text

javascript - js parses multi-layer nested json data, recursive call

Need to process the json data returned by the interface and convert it into a data format that Easyui Tree can receive. How to process multi-layer json data. The size of the json data returned is not necessarily certain, and the attributes also change. The structure is similar, only @desc is the root directory.

example:
{

"srvcfg": {
    "tokenswitch": {
        "token_switch": {
            "#text": "0",
            "@desc": "切换开关"
        },
        "@desc": "token切换"
    },
    "commoncfg": {
        "srvtype": {
            "count": {
                "#text": "1",
                "@desc": "数量"
            },
            "srvtypeid1": {
                "#text": "28900",
                "@desc": "类型"
            },
            "@desc": "SrvTypeID的数量"
        },
        "binanry_msg_queue": {
            "count": {
                "#text": "2",
                "@desc": "消息队列数量"
            },
            "queuenum1": {
                "#text": "10000",
                "@desc": "消息队列号"
            },
            "queuenum2": {
                "#text": "20000",
                "@desc": "消息队列最小长度"
            },
            "queuesize1": {
                "#text": "300",
                "@desc": "小消息队列"
            },
            "queuesize2": {
                "#text": "10000",
                "@desc": "消息队列最大长度"
            },
            "@desc": "消息队列配置"
        },
        "srv_pwd": {
            "count": {
                "#text": "1",
                "@desc": "约定密码数量"
            },
            "srvtypeid1": {
                "#text": "28900",
                "@desc": "端口"
            },
            "pwd1": {
                "#text": "dtvspwd",
                "@desc": "dtvs密码"
            },
            "@desc": "服务器之间约定的密码"
        },
        "@desc": "这是SrvTypeID规定"
    }
}

}

学习ing学习ing2644 days ago1016

reply all(1)I'll reply

  • 高洛峰

    高洛峰2017-06-28 09:24:18

    First of all, let’s analyze it. No matter what the data structure is, it must be analyzed by humans and machines through type.

    1. There are types in it, such as a, b, c, etc. If so, we can definitely set up different parsing solutions for different types.

    2. There is no clear type to identify it. It uses name. Name and age are definitely two different things, so we can judge by key.

    For things in json that are not naming rules, we can use [] to access them

    reply
    0
  • Cancelreply