codeMirror中JSON 数据,定位某个属性在第几行,

json内容赋值到codeMirror编辑器中使用

第一种方法

var jsonObj = { "OrdererOrgs": [ { "Name": "orderer", "Domain": "example1.com", "Specs": [ { "Hostname": "orderer" } ] } ], "PeerOrgs": [ { "Name": "org1", "Domain": "org1.example1.com", "Specs": [ { "Hostname": "peer0" } ], "Users": { "Count": 1 } }, { "Name": "org2", "Domain": "org2.example2.com", "Specs": [ { "Hostname": "peer3" } ], "Users": { "Count": 1 } } ] }

editor.setValue(JSON.stringify(jsonObj, null, 4))

JSON.stringify()能让json对象,格式化内容,将数据分成一行行

codeMirror中JSON 数据,定位某个属性在第几行,_第1张图片

根据codeMirror.lineInfo(i).text 来循环出行内容对应行数

var prop = "OrdererOrgs[0]|Domain"  // 需要查找的属性名格式

var lineTotal1 = jsonEditor1.lineCount(),
     arrObject1 = {}; 
     for(var i=0;i

根据codeMirror.getValue()  来循环出行内容对应行数

//var jsonData ={"OrdererOrgs":[{"Name":"orderer","Domain":"example1.com","Specs":[{"Hostname":"orderer"}]}],"PeerOrgs":[{"Name":"org1","Domain":"org1.example1.com","Specs":[{"Hostname":"peer0"}],"Users":{"Count":1}},{"Name":"org2","Domain":"org2.example2.com","Specs":[{"Hostname":"peer3"}],"Users":{"Count":1}}]}
//$("#content").val(JSON.stringify(jsonData,null,4))
// 注销的内容,是无需codeMirror编辑器,只需textarea元素
var prop2 = "OrdererOrgs[0]|Name";
//var getJsonData2 = $("#content").val();
var getJsonData2 = jsonEditor1.getValue();
console.log(JSON.stringify(getJsonData2))

var jsonObjTest = JSON.stringify(getJsonData2).slice(1,JSON.stringify(getJsonData2).length-1).replace(/\\n/g,"|").split("|"),
	jsonObjTestLen = jsonObjTest.length;

var i=0,
    jsonReintegration = {};
	
for(i;i

searchJsonPropertyLine 函数js

function searchJsonPropertyLine(prop,lineToTextObject){   // 查找JSON的属性行数

    var getPropertyString = prop

    // var getPropertyString = "PeerOrgs"
    var propArr = getPropertyString.split("|")
    var indexI = 0;
    var objectKey,
        objReg = /\[\d+\]/;

    var arrString = lineToTextObject,
        result,
        nextObjectNumber;
console.log("test" + arrString)
   

    while(propArr[indexI] !== undefined){

      objectKey = propArr[indexI]
      var numCheck = Boolean;
      var objectKeyLen = objectKey.length,
          positionLastIndex = [],
          sliceString,
          lineNum;
          numCheck = objReg.test(objectKey);

      if(numCheck){   // 有下标,例如[0]

        var objectCut1 = objectKey.slice(0,objectKeyLen - 1), // 去掉最后的"]"
            objectCut2 = objectCut1.lastIndexOf("[") , // 定位‘[’,
            objectCut3 = objectKey.slice(0,objectCut2); // 获取属性名

            propStringLen = objectCut3.length

        var objectKeyLen2 = objectCut3.length + 9;

        var indexArr = objectKey.slice(objectCut2 + 1,objectKeyLen - 1)  // 获取下标签


        var matchReg = '\\\\"' + objectCut3 +'\\\\":';

        var pattReg = new RegExp(matchReg,"g");

        if(sliceString == undefined){
            sliceString = arrString;
        }


        while ((result = pattReg.exec(sliceString)) != null)  {
          positionLastIndex.push(pattReg.lastIndex)
        }


        if(propArr.length == indexI+1){  // 判断属性名是不是最后一个

          var frontString = sliceString.slice(0,positionLastIndex[0]- propStringLen - 9),
              quotePosition = frontString.lastIndexOf('"');

              lineNum = Number(frontString.slice(quotePosition + 1,frontString.length)) + Number(indexArr) + 1;

              sliceString = sliceString.slice(quotePosition,sliceString.length)

        }else{
          nextObjectNumber = nextObjectNumber == undefined? 0 : nextObjectNumber;

          if(positionLastIndex[nextObjectNumber+1] == undefined){

            var frontString = sliceString.slice(0,positionLastIndex[nextObjectNumber]- propStringLen - 9),
                quotePosition = frontString.lastIndexOf('"');

                lineNum = frontString.slice(quotePosition + 1,frontString.length);

                sliceString = sliceString.slice(quotePosition,sliceString.length);

          }else{

            var frontString = sliceString.slice(0,positionLastIndex[nextObjectNumber] - propStringLen - 9),
                quotePosition = frontString.lastIndexOf('"'),
                lineLen ;

                lineNum = frontString.slice(quotePosition + 1,frontString.length)

                lineLen = lineNum.length
                sliceString = sliceString.slice(positionLastIndex[nextObjectNumber] - propStringLen - lineLen - 10,positionLastIndex[nextObjectNumber+1]- lineLen - propStringLen - 10)

          }

        }


        // console.log(positionLastIndex)
        // var frontString = sliceString.slice(0,positionLastIndex[0] - objectKeyLen2)
        // console.log(frontString)
        // sliceString = sliceString.slice(frontString.lastIndexOf('"'),-1)
        // lineNum = frontString.slice(frontString.lastIndexOf('"')+1,frontString.length)
        nextObjectNumber = Number(indexArr);

      }else{       // 没有下标文件

        var matchReg = '\\\\"' + objectKey +'\\\\":'
            arrNumber="",
            result;

        var propStringLen = objectKey.length
        var pattReg = new RegExp(matchReg,"g");

        if(sliceString == undefined){
            sliceString = arrString;

        }
        while ((result = pattReg.exec(sliceString)) != null)  {
          positionLastIndex.push(pattReg.lastIndex)
        }
        // arrNumber = Number(nextObjectNumber+1)
        // console.log(arrNumber);


        if(positionLastIndex[nextObjectNumber+1] == undefined){ // 没有重复的元素

          nextObjectNumber = nextObjectNumber == undefined? 0 : nextObjectNumber;
          var frontString = sliceString.slice(0,positionLastIndex[nextObjectNumber]- propStringLen - 9),
              quotePosition = frontString.lastIndexOf('"');

              lineNum = frontString.slice(quotePosition + 1,frontString.length);

              sliceString = sliceString.slice(quotePosition,sliceString.length)

        }else{   //有重复的元素

          nextObjectNumber = nextObjectNumber == undefined? 0 : nextObjectNumber;

          var frontString = sliceString.slice(0,positionLastIndex[nextObjectNumber] - propStringLen - 9),
              quotePosition = frontString.lastIndexOf('"'),
              lineLen ;
              lineNum = frontString.slice(quotePosition + 1,frontString.length)
              console.log(lineNum +"line4")
              lineLen = lineNum.length

          // console.log(sliceString.slice(0,positionLastIndex[nextObjectNumber] - propStringLen - 9))
          // console.log(sliceString.slice(0,positionLastIndex[nextObjectNumber+1] - propStringLen - 9))
          sliceString = sliceString.slice(positionLastIndex[nextObjectNumber] - propStringLen - lineLen - 10,positionLastIndex[nextObjectNumber+1]- lineLen - propStringLen - 10)



        }

        nextObjectNumber = undefined;
      }
      // console.log(lineNum+"line")
      indexI++;
    }
    return Number(lineNum)+1;

}

 

你可能感兴趣的:(jquery,js插件)