简体   繁体   English

使用Jquery或Javascript将字符串数组转换为特定的JSON格式

[英]Converting Array of String in a particular JSON Format using Jquery or Javascript

I am having a javascript array in this particular format: 我有这种特定格式的javascript数组:

var arr  = 
  ["header1",       //section header
   false,           //subsection, no
   "1240","1243",   //assets
   true,"1",        //subsection, yes = 1
   "1344","1136",   //assets
   true,"1",        //subsection, yes = 1
   "1347",          //assets
   "header2",       //section header
   false,           //subsection, no
   "1130"];         //assets

The above array is having a sequence: 上面的数组有一个序列:

1) The array with "header" is the section value. 1)具有“ header”的数组是段值。

2) "false" in the array indicates that this section is not having any subsection. 2)数组中的“ false”表示此部分没有任何子部分。 So in JSON the sub value is null. 因此,在JSON中,子值为null。

3) Followed by false are all the assets value for the sections. 3)后面跟着是该节的所有资产值。

4) "true" indicates that this section is having a subsection. 4)“ true”表示此部分包含一个子部分。 The subsection value in the next value followed by "true". 下一个值中的小节值,后跟“ true”。 In my example it is 1. Following it are all assets values for that subsection. 在我的示例中为1。其后是该小节的所有资产值。

5) When the next array value with string "header" is encountered. 5)当遇到带有字符串“ header”的下一个数组值时。 It is start of the next section. 这是下一部分的开始。

I have to convert it into the format: 我必须将其转换为格式:

{
  "templateForm": 
    [{
      "sectionId":"header1",
      "values":[
        {
          "sub":null,
          "assets":[1240,1243]
        },
        {
          "sub":1,
          "assets":[1344,1136]
        },
        {
          "sub":1,
          "assets":[1347]
        }
        ]
    },
    {
      "sectionId":"header2",
      "values":[
        {
          "sub":null,
          "assets":[1130]
        }
        ]
    }]
}

I had tried a lot but not able to do it. 我尝试了很多,但无法做到。 I tried to create the json format as string but got error at the time of parsing it as javascript object. 我试图将json格式创建为字符串,但是在将其解析为javascript对象时出现错误。 Please help me to solve this problem. 请帮我解决这个问题。

My incomplete code are as follows: 我不完整的代码如下:

function makeJSON(formItems) {
        var subString1 = "header";
        var strStart = '{"templateForm":[';
        var strSection = "";
        for (var i = 0; i < formItems.length; i++) {
            var isHeader = item.indexOf(subString1) !== -1;
            if(isHeader){
                strSection += '{"sectionId":'+item[i];
                while(item != true || item != false){
                }
            }

            var item = formItems[i] + "";
            console.log(item);
            if (item == "true") {
                var subSectionId = item[++i];
            } else if (item == "false") {
                var subSectionId = null;
            }
        }
        var strEnd = "]}";
        var strFinal = strStart + strSection + strEnd;
        console.log(strFinal);
        var obj = JSON.parse(strFinal);
        console.log(obj);
    }

You could use a straight forward approach with an object for the single stages of inserting. 对于插入的单个阶段,可以对对象使用简单的方法。

 var array = ["header1", false, "1240", "1243", true, "1", "1344", "1136", true, "1", "1347", "header2", false, "1130"], result = [], last = {}; array.forEach(function (a) { if (a.toString().slice(0, 6) === 'header') { last.section = { sectionId: a, values: [] }; result.push(last.section); return; } if (typeof a === 'boolean') { last.sub = { sub: null, assets: [] }; last.section.values.push(last.sub); last.next = a; return; } if (last.next) { last.sub.sub = +a; last.next = false; return; } last.sub.assets.push(+a); }); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

The format of the data source leaves much to desire for. 数据源的格式有很多不足之处。 But if this is what you must work with, then following code can make the conversion: 但是,如果这是您必须使用的方法,那么可以使用以下代码进行转换:

 var arr = ["header1",false,"1240","1243",true,"1","1344","1136",true,"1","1347", "header2", false, "1130"]; var result = arr.reduce( (acc, val) => { if (String(val).indexOf("header") === 0) { acc.push({ sectionId: val, values: [] }); return acc; } var last = acc[acc.length-1], lastVal = last.values[last.values.length-1]; if (typeof val === 'boolean') { last.values.push({ sub: val || null, assets: [] }) } else if (lastVal.sub === true) { lastVal.sub = +val; } else { lastVal.assets.push(+val); } return acc; }, []); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM