美文网首页
Node.js 数据格式同时支持JSON和HL7

Node.js 数据格式同时支持JSON和HL7

作者: 环零弦 | 来源:发表于2017-08-16 16:49 被阅读0次

Node.js 数据格式同时支持JSON和HL7

一个正常的HL7格式的数据大概长这样:

MSH|^~\&|HIS|SFPH|NIS|SFPH|20140626114850.755|N|ZK~S^Z01^ZKS_Z01|NISGetHisDept-20140626114850755|P|2.7
Z01|0|部门ID|部门名称|部门性质|实有床位|开放床位数|预缴金下限,第一次预缴金的下限|催款下限,病区提示催款的下限|记账下限,病区病人实际余额的下限|允许欠费上限|对应门诊科室|对应住院科室|病区急诊床位数|科室共享床位数|拼音首码|五笔首码|操作人|操作时间

经过一番阅读HL7的标准,成功地总结出HL7就是一堆段头领着一帮段信息,一级隔断用|,二级隔断用^,三级隔断用~,别的就随意了。具体内部消息格式自己定义。于是,在当下阶段,先把HL7与JSON格式之间的互相转换做出来就好,以后遇到正式的业务逻辑再做相应更改。

let hl7Body = {};
let hl72Json = (hl7Str) => {
    let payLoadArray = hl7Str.split('\r\n');
    for (let countX = 0; countX < payLoadArray.length; countX++) {
        hl7Body[payLoadArray[countX].substring(0, 3)] = payLoadArray[countX].substring(4).split('|');
        for (let countY = 0; countY < hl7Body[payLoadArray[countX].substring(0, 3)].length; countY++) {
            // console.log(hl7Body[payLoadArray[countX].substring(0, 3)][countY] + ':' + hl7Body[payLoadArray[countX].substring(0, 3)][countY].indexOf('^'));
            if (hl7Body[payLoadArray[countX].substring(0, 3)][countY].indexOf('^') !== -1 && ((payLoadArray[countX].substring(0, 3) !== 'MSH') || (countY !== 0))) {
                hl7Body[payLoadArray[countX].substring(0, 3)][countY] = hl7Body[payLoadArray[countX].substring(0, 3)][countY].split('^');
                if (Array.isArray(hl7Body[payLoadArray[countX].substring(0, 3)][countY])) {
                    for (let countZ = 0; countZ < hl7Body[payLoadArray[countX].substring(0, 3)][countY].length; countZ++) {
                        if (hl7Body[payLoadArray[countX].substring(0, 3)][countY][countZ].indexOf('~') !== -1) {
                            hl7Body[payLoadArray[countX].substring(0, 3)][countY][countZ] = hl7Body[payLoadArray[countX].substring(0, 3)][countY][countZ].split('~');
                        }
                    }
                }
            }

        }
    }
    setTimeout(() => {
        console.log(hl7Body);
        // console.log(hl7Body.MSH);
        json2Hl7(hl7Body);
    }, 500);
}
let json2Hl7 = (jsonObj) => {
    let hl7Str = '';
    for (let x in jsonObj) {
        hl7Str = hl7Str + '\r\n' + x + '|' + getResult(jsonObj[x], 1);
    }
    setTimeout(() => {
        // console.log(hl7Str.substring(2));
    }, 500)
}

let getResult = (inputParam, level) => {
    let tmpStr = '';
    switch (level) {
        case 1:
            if (Array.isArray(inputParam)) {
                let tmpCount = 0;
                for (let tmpCell in inputParam) {
                    if (++tmpCount < inputParam.length) {
                        tmpStr = tmpStr + getResult(inputParam[tmpCell], 2) + '|';
                    } else {
                        return tmpStr + getResult(inputParam[tmpCell], 2);
                    }
                }
            } else {
                return inputParam;
            }
            break;
        case 2:
            if (Array.isArray(inputParam)) {
                let tmpCount = 0;
                for (let tmpCell in inputParam) {
                    if (++tmpCount < inputParam.length) {
                        tmpStr = tmpStr + getResult(inputParam[tmpCell], 3) + '^';
                    } else {
                        return tmpStr + getResult(inputParam[tmpCell], 3);
                    }
                }
            } else {
                return inputParam;
            }
            break;
        case 3:
            if (Array.isArray(inputParam)) {
                let tmpCount = 0;
                for (let tmpCell in inputParam) {
                    if (++tmpCount < inputParam.length) {
                        tmpStr = tmpStr + getResult(inputParam[tmpCell], 4) + '~';
                    } else {
                        return tmpStr + getResult(inputParam[tmpCell], 4);
                    }
                }
            } else {
                return inputParam;
            }
            break;
        default:
            return inputParam;
    }
}


module.exports = {
    hl72Json: hl72Json,
    json2Hl7: json2Hl7
}

调用如下:

let fs = require('fs');
let path = require('path');
let handler = require('./main_sim_4');

// -----------------------------------------------------------------------

function test(filename) {
    var filepath = path.normalize(path.join(__dirname, filename));
    fs.readFile(filepath, 'utf8', function (err, xmlStr) {
        let jsonBody = handler.hl72Json(xmlStr);
        // console.log('======================')
        //console.log(jsonBody);

        //let xml = handler.json2xml(jsonBody);
        //console.log(xml);
    });
};

test('hl7payload2.txt');

相关文章

  • Node.js 数据格式同时支持JSON和HL7

    Node.js 数据格式同时支持JSON和HL7 一个正常的HL7格式的数据大概长这样: 经过一番阅读HL7的标准...

  • Axios 同时支持下载和 JSON 数据格式

    一、需求 后端导入Excel之后,以Excel的方式反馈每一条处理的结果,但有时可能上传的文件错误或者权限限制的错...

  • 数据格式

    XML和JSON数据格式 json数据格式 {"":"", "":"", "":""} json xml对比 1....

  • 零基础带你搞定分布式爬虫(第二节)

    数据存储 json ------命名不要json.py,坑----- JSON支持数据格式: 对象(字典)。使用花...

  • Spring同时支持Json和Xml

    项目中有时候需要同时支持XML和JSON格式的参数和返回值,如果是参数还比较容易处理,可以用String接收然后手...

  • 科曼监护仪HL7对接

    监护仪维护密码 5188 科曼监护仪支持 HL7, OEM 等协议。可以直接设置一个 HL7 服务端的IP 和端...

  • day12-json和异常捕获

    1.json数据 1.什么是json数据json是一种数据格式,满足json数据格式就是json数据(json文件...

  • JS之改变数组对象的属性名

    JSON数据格式前提:JSON.stringfy实例如下 JSON.parse实例如下 非JSON数据格式Arra...

  • com.mysql.jdbc.MysqlDataTruncati

    mysql5.7之后开始支持json数据格式类型。但是mybaits暂时不支持。逆向工程生成的entity的字段类...

  • json

    和js对象的区别 json只是一种数据格式,不支持undefined,字符串必须使用双引号,需要对/进行转义/。 ...

网友评论

      本文标题:Node.js 数据格式同时支持JSON和HL7

      本文链接:https://www.haomeiwen.com/subject/donirxtx.html