根据数据中的参数对json数据进行分组

时间:2017-06-28 12:18:18

标签: c# json

我有一个json结果,如image1所示 enter image description here

但是我需要一个输出,如image2所示 enter image description here

正如您从图像中看到的那样,隔离是基于最后一个尾随数字,使用我们可以分组的尾随数字。

任何人都可以提供帮助,我该如何进行分组。

我得到的结果是

    {"MessageTable":
{"message-Code-1":"    ",
"msg-Number-Pos1-1":" ",
"msg-Number-Pos2-1":" ",
"msg-Number-Pos3-1":" ",
"message-Code-2":"    ",
"msg-Number-Pos1-2":" ",
"msg-Number-Pos2-2":" ",
"msg-Number-Pos3-2":" ",
"message-Code-3":"    ",
"msg-Number-Pos1-3":" ",
"msg-Number-Pos2-3":" ",
"msg-Number-Pos3-3":" ",
"message-Code-4":"    ",
"msg-Number-Pos1-4":" ",
"msg-Number-Pos2-4":" ",
"msg-Number-Pos3-4":" ",
"message-Code-5":"    ",
"msg-Number-Pos1-5":" ",
"msg-Number-Pos2-5":" ",
"msg-Number-Pos3-5":" "}}

必需的json格式

{
   "message-Table": [
            {
              "message-Code-1": "0000",
              "msg-Number-Pos1": "1",
              "msg-Number-Pos2": "1",
              "msg-Number-Pos3": "1"
            },
            {
              "message-Code-2": "0001",
              "msg-Number-Pos1": "2",
              "msg-Number-Pos2": "1",
              "msg-Number-Pos3": "1"
            },
            {
              "message-Code-3": "0002",
              "msg-Number-Pos1": "2",
              "msg-Number-Pos2": "1",
              "msg-Number-Pos3": "1"
            },
            {
              "message-Code-4": "0004",
              "msg-Number-Pos1": "2",
              "msg-Number-Pos2": "1",
              "msg-Number-Pos3": "1"
            },
            {
              "message-Code-5": "0005",
              "msg-Number-Pos1": "2",
              "msg-Number-Pos2": "1",
              "msg-Number-Pos3": "1"
            }
          ]

}

由于

1 个答案:

答案 0 :(得分:0)

冒着我可能为你做功课的风险,这里有一些javascript可以执行你正在寻找的转换。



function convertMessageTable(sourceText){
    var sourceObj = JSON.parse(sourceText);
    var newObj = {'message-Table':[]};
    var code=0;
    var message = undefined;
    for (var key in sourceObj['MessageTable']) {
        var element = sourceObj[key];
        if(key.startsWith('message-Code')){
            if(message){
                newObj['message-Table'].push(message);            
            }
            var codeString = `0000${code++}`.substr(-4);        
            message = {};
            message[key]=codeString;
        }
        else{
            var msg = parseMessage(key);
            message[msg.key]=msg.value;
        }
    }
    
    return JSON.stringify(newObj,null,2);
}

function parseMessage(messageName){
    var key = messageName.substr(0,messageName.length - 2);
    var value = messageName.match(/[0-9]+$/)[0];
    return {'key':key,'value':value};
}

var sourceText =    `{"MessageTable":
{"message-Code-1":"    ",
"msg-Number-Pos1-1":" ",
"msg-Number-Pos2-1":" ",
"msg-Number-Pos3-1":" ",
"message-Code-2":"    ",
"msg-Number-Pos1-2":" ",
"msg-Number-Pos2-2":" ",
"msg-Number-Pos3-2":" ",
"message-Code-3":"    ",
"msg-Number-Pos1-3":" ",
"msg-Number-Pos2-3":" ",
"msg-Number-Pos3-3":" ",
"message-Code-4":"    ",
"msg-Number-Pos1-4":" ",
"msg-Number-Pos2-4":" ",
"msg-Number-Pos3-4":" ",
"message-Code-5":"    ",
"msg-Number-Pos1-5":" ",
"msg-Number-Pos2-5":" ",
"msg-Number-Pos3-5":" "}}`;

var output = convertMessageTable(sourceText);
console.log(output);