json walkthrough creating multi tier json objects or arrays

Today, I met a problem about classification, I want to use json for storage, but I haven't used it very much before, and I encountered a series of problems.

First, I will introduce some basic usage:

1. Definition of json var json = {}; / / json object, storing key value pairs

var json = [{}, {}] / / json array

2. The key value pair is assigned to the OK key name json. Name = "Mary", the json value {"name", "Mary"}

Variable: var key = "123547d468ds1f68sd" json[key]="98sf65648fs864fd"

json value {"123547d468ds1f68sd","98sf65648fs864fd"}

3. json value json array:

var jsonStr = '[{"id":"01","open":false,"pId":"0","name":"Hypothetical Department"},
{"id":"01","open":false,"pId":"0","name":"Hypothetical Department"},
{"id":"011","open":false,"pId":"01","name":"Hypothetical Department"},
{"id":"03","open":false,"pId":"0","name":"Hypothetical Department"},
{"id":"04","open":false,"pId":"0","name":"Hypothetical Department"},
{"id":"05","open":false,"pId":"0","name":"Hypothetical Department"}, 
{"id":"06","open":false,"pId":"0","name":"Hypothetical Department"}]';
var jsonObj =  JSON.parse(jsonStr);//Convert to json object
for(var i=0;i<jsonObj.length;i++){
        alert(jsonObj[i].id);  //Take the value in json
}


//json string to json object
1  var jsonString = '{"bar":"property","baz":3}';
2   var jsObject = JSON.parse(jsonString);    //Convert to json object
    //Or var jsObject eval('(' + jsonString + ')');
//The first is:
3   alert(jsObject.bar);    //Take the value in json
//Second species:
4   for(var i in jsObject){
        console.info(jsObject[i]);  //The i here is not the number one, but the corresponding key, such as i="bar";
    }

Difference: the eval method does not check whether the given string conforms to the json format, while parse will check whether the given string conforms to the json format, and an error will be reported if the given string does not conform to the json format. At the same time, if there is a js code in the given string, Eval will also be executed at the same time.

Make some basic explanations according to the actual application:

//Implementation target form: {"key1":{"key11": [], "key12": [], "key13": [],...},
               "key2":{"key21":[],"key22":[],"key23":[],...},...},adopt key1 You can find the right one json,adopt key2 Find the corresponding array
						var json = {};
						var arrJson = new Array();
//rcvJOSNLine is a json array, which stores relevant information
						for(var j=0,leng=rcvJOSNLine.length;j<leng;j++){	
							//Get the objects in the array. rcvLineObj contains key1, key2 and array values
							var rcvLineObj = rcvJOSNLine[j];
//To obtain the temporary objects of key1 and key2, do not pay attention
							var tmpStr = getRcvVoListData(rcvLineObj.receiptNumber,data.rcvVoListData);
//Get key1
							var rcvPoHeaderId = tmpStr.split(",")[1];   //Search Po? ID according to the receipt doc No
//Get key2 temporary variable    
							var costTemp = rcvLineObj.budgetName;
//Get key2
							var costCenter;
							if(costTemp[0].indexOf('A') == -1  && costTemp[0].indexOf('B') == -1 ){
								costCenter = costTemp.split(".")[2];     //Opex type intercepts the third segment of budgetName, which is the cost center
							}else{
								costCenter = costCenterAll[tmpStr.split(",")[5]];   //Receipt doc No. --- Department ID -- > costcenter, capex type cost center is the Department
							}

//Judge whether key1 is empty, create the whole object if it is empty, and push the value into the array,
							if(json[rcvPoHeaderId] == null){
								var poLineId = new Array();
								var jn = {};
								poLineId.push(rcvLineObj.poLineId);
//This is to create {"key2": []}
								jn[costCenter] = poLineId;
//This is to create {"key1":{"key2": []}}
								json[rcvPoHeaderId] = jn;
								    //key1 is not empty,
							}else if(json[rcvPoHeaderId] != null){
                                    //key2 is empty. json objects under the same key1 have key value pairs
								if(json[rcvPoHeaderId][costCenter] == null){
									var poLineId = new Array();
//To ensure that there are multiple key value pairs under the same object, get the value of key1, and then create a new {"key2": []}, rather than a new one
//var jn = {}; if it is newly created, it will overwrite the previous {"key2": []}, resulting in only one key2 for json
									var jn = json[rcvPoHeaderId];
									poLineId.push(rcvLineObj.poLineId);
									jn[costCenter] = poLineId;
									json[rcvPoHeaderId] = jn;
								}else if(json[rcvPoHeaderId][costCenter] != null){
									json[rcvPoHeaderId][costCenter].push(rcvLineObj.poLineId);
								}
							}

						}

 

Keywords: JSON

Added by bsgrules on Sat, 04 Jan 2020 04:32:10 +0200