利用js解析php的表單資料

usher.yue發表於2017-09-07

利用js解析php的表單資料

/**
 * JsonParse
 *  Created by usher.yue.
 * User: usher.yue
 * Date: 17/9/7
 * Time: 13:50
 * 心懷教育夢-煙臺網格軟體技術有限公司
 */
var obj = {
    `data[][classid][0]`: `1000150097620084286`,
    `data[][create_time][0]`: `1504692288`,
    `data[][kcid][0]`: `1033150427887550927`,
    `data[][schoolid][0]`: `1033144479144228284`,
    `data[][type][0]`: `2`,
    `data[][uid][0]`: `1034145308280458231`,
    `data[][zyid][0]`: `1000150235157063544`,
    `data[][zytype][0]`: `homework-exercise`,

    `data[][classid][1]`: `10001500976200842861`,
    `data[][create_time][1]`: `1504692288`,
    `data[][kcid][1]`: `1033150427887550927`,
    `data[][schoolid][1]`: `1033144479144228284`,
    `data[][type][1]`: `2`,
    `data[][uid][1]`: `1034145308280458231`,
    `data[][zyid][1]`: `1000150235157063544`,
    `data[][zytype][1]`: `homework-exercise`,

    `data[0][classid][2]`: `10001500976200842861`,
    `data[0][create_time][2]`: `1504692288`,
    `data[0][kcid][2]`: `1033150427887550927`,
    `data[0][schoolid][2]`: `1033144479144228284`,
    `data[0][type][2]`: `2`,
    `data[0][uid][2]`: `1034145308280458231`,
    `data[0][zyid][2]`: `1000150235157063544`,
    `data[0][zytype][2]`: `homework-exercise`,

    `data[0][classid][3]`: `10001500976200842861`,
    `data[0][create_time][3]`: `1504692288`,
    `data[0][kcid][3]`: `1033150427887550927`,
    `data[0][schoolid][3]`: `1033144479144228284`,
    `data[0][type][3]`: `2`,
    `data[0][uid][3]`: `1034145308280458231`,
    `data[0][zyid][3]`: `1000150235157063544`,
    `data[0][zytype][3]`: `homework-exercise`
};


class Xue12JsonConvert {
    /**
     * deep merge
     * @param target
     * @param source
     * @returns {*}
     * @private
     */
    __d(target, source) {
        for (var k in source) {
            if (target[k] && target[k].toString() === "[object Object]") {
                this.__d(target[k], source[k])
            }
            else {
                if (this.__type(target[k]) === "[object Array]") {
                    //去空合併
                    if (this.__type(source[k]) === "[object Array]") {
                        for (let item of source[k]) {
                            item && target[k].push(item);
                        }
                    }else{
                        target[k].push(source[k]);
                    }
                } else {
                    target[k] = source[k];
                }
            }
        }
        return target;
    }

    /**
     * json parser
     * @param obj
     * @param keyList
     * @param deepVal
     * @returns {*}
     * @private
     */
    __parseToJson(obj, keyList, deepVal) {
        let retObj = null;
        if (!keyList) {
            let re = /([_a-zA-Z]w+|[]|[[0-9]+]|[[_a-zA-Z]w*])/g;
            let combineArr = [];
            let combineObj={};
            for (var key in obj) {
                retObj = {};
                //解析key
                let matchSubKey = re[Symbol.match](key);
                let indexMatch = /^[([0-9]+)]$/.exec(matchSubKey[1]);
                let noneIndexMatch = /^[]$/.exec(matchSubKey[1]);
                let objectMatch = /^[([a-zA-Z_]w+)]$/.exec(matchSubKey[1]);
                if (indexMatch) {
                    retObj[matchSubKey[0]] = [];
                } else if (noneIndexMatch) {
                    retObj[matchSubKey[0]] = [];
                } else if (objectMatch) {
                    retObj[matchSubKey[0]] = {};
                }
                //賦值
                retObj[matchSubKey[0]] = this.__parseToJson(null, matchSubKey.slice(1), obj[key]);
                //合併物件
                combineObj=this.__d(combineObj,retObj);
            }
            //解析資料
            return combineObj;
        } else {
            if (keyList.length > 1) {
                let indexMatch = /^[([0-9]+)]$/.exec(keyList[0]);
                let noneIndexMatch = /^[]$/.exec(keyList[0]);
                let objectMatch = /^[([a-zA-Z_]w+)]$/.exec(keyList[0]);
                if (indexMatch) {
                    retObj = [];
                    retObj[indexMatch[1]] = this.__parseToJson(null, keyList.slice(1), deepVal);
                } else if (noneIndexMatch) {
                    retObj = [];
                    retObj[noneIndexMatch[1]] = [];
                    retObj.push(this.__parseToJson(null, keyList.slice(1), deepVal));
                } else if (objectMatch) {
                    retObj = {};
                    retObj[objectMatch[1]] = {};
                    retObj[objectMatch[1]] = this.__parseToJson(null, keyList.slice(1), deepVal);
                }
            } else if (keyList.length == 1) {
                let indexMatch = /^[([0-9]+)]$/.exec(keyList[0]);
                let noneIndexMatch = /^[]$/.exec(keyList[0]);
                let objectMatch = /^[([a-zA-Z_]w+)]$/.exec(keyList[0]);
                if (indexMatch) {
                    retObj = [];
                    retObj[indexMatch[1]] = deepVal;
                } else if (noneIndexMatch) {
                    retObj = [];
                    retObj.push(deepVal);
                } else if (objectMatch) {
                    retObj[objectMatch[1]] = deepVal;
                }
            }
        }
        return retObj;
    }

    /**
     * type
     * @param o
     * @returns {*}
     * @private
     */
    __type(o) {
        return Object.prototype.toString.call(o);
    }

    /**
     * 轉換
     * @param obj
     * @returns {*}
     */
    parseToJson(obj) {
        return this.__parseToJson(obj);
    }
}


let json = new Xue12JsonConvert();
let r = json.parseToJson(obj);
console.log(JSON.stringify(r))


相關文章